Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

where to define eventhandler

 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have a class called POFrame which has instance variables:
1. Panel1
2.Panel2
3.Panel3
4.Panel4
5.Panel5.
Panel3 encloses Panel1 and Panel2.
In Panel1 there is a Choice object where i have
1. Option1
2. Option2.
There is a List Object in Panel2. When i create it , i populate it with 3 items corresponding to the Option1 in Panel1.
Now, here is my problem:
If after the frame is displayed, the user selects Option2 (an ItemEvent), then i should change the 3 items in the list in Panel2 corresponding to the new Option. How do i go about it?
I tried to remove the List from the Panel2 and repopulate it and then add it.
Also, where should i write the eventhandler? I wrote it in a separate class, but when i compile it gives me the error that it is not able to find Panel1,Panel2,and so on, though Panel1...Panel5 are all default instance variables of the POFrame class.
Rgds,
Shree
p.s. If i have not made a good job of explaining the problem, i
will post the program... I guessed people wouldn't prefer going through a lot of lines of code...
 
shree vijay
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Shown below is the whole program:
If i remove the comments , the program doesn't compile. I tried quite a bit to solve this on my own, but couldn't. Also, am i doing it right when i am defining the event handler as i have done here? All the code gets packed into the constructor and this class itself, i sense something awkward about it.
/* Creating a purchase order screen */
import java.awt.*;
import javax.swing.*;
import java.text.*;
import java.util.*;

public class POFrame extends JFrame
{
public Panel leftPanel;
public Panel rightPanel;
public Panel topPanel;
public Panel paymentPanel;
public Panel submitCancelPanel;


// constructor
public POFrame()
{
super("Purchase Order");

setBounds(50,50,600,500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
getContentPane().setLayout(new GridLayout(3,0));


leftPanel=new Panel();
rightPanel=new Panel();
topPanel=new Panel();
paymentPanel=new Panel();
submitCancelPanel=new Panel();

// Filling the TopPanel

leftPanel.setLayout(new GridLayout(4,1));
leftPanel.add(new Label("Date"));
Date today = new Date();
DateFormat df = DateFormat.getDateInstance(DateFormat.LONG,Locale.US);
String prntDate = df.format(today);
TextField dateText = new TextField();
dateText.setText(prntDate);
leftPanel.add(dateText);
leftPanel.add(new Label("ITEM CATEGORY"));
Choice item_category = new Choice();
item_category.addItem("Hardware");
item_category.addItem("Software");

// CategoryListener cl = new CategoryListener();
//item_category.addItemListener(cl);

leftPanel.add(item_category);


rightPanel.setLayout(new GridLayout(4,1));
rightPanel.add(new Label("ITEM DESCRIPTION"));
rightPanel.add(new TextArea());
rightPanel.add(new Label("ITEM COMPONENT"));

CompList cList = new CompList("Hardware");

rightPanel.add(cList);

topPanel.setLayout(new GridLayout(1,2));
topPanel.add(leftPanel);
topPanel.add(rightPanel);

paymentPanel.setLayout(new GridLayout(4,1));
paymentPanel.add(new Label("PAYMENT METHOD"));

CheckboxGroup cbg=new CheckboxGroup();
Checkbox cbx1=new Checkbox("Visa",false,cbg);
Checkbox cbx2=new Checkbox("MasterCard",false,cbg);
Checkbox cbx3=new Checkbox("American Express",false,cbg);
// paymentPanel.add(cbg);

paymentPanel.add(cbx1);
paymentPanel.add(cbx2);
paymentPanel.add(cbx3);



// Filling the submitCancelPanel

// add Buttons

Button CancelButton = new Button("Cancel");
Button SubmitButton = new Button("Submit");

CancelButton.setBackground(Color.blue);
SubmitButton.setBackground(Color.red);

submitCancelPanel.add(CancelButton);
submitCancelPanel.add(SubmitButton);


getContentPane().add(topPanel);
getContentPane().add(paymentPanel);
getContentPane().add(submitCancelPanel);
setVisible(true);


}


}

/* class CategoryListener implements ItemListener
{
public void itemStateChanged(ItemEvent ie)
{
Choice catgList = (Choice)(ie.getSource());
String catgChosen = catgList.getSelectedItem();
if (catgChosen.equals("Hardware"))
{
rightPanel.removeAll();
rightPanel.add(new Label("ITEM COMPONENT"));
CompList cList = new CompList("Hardware");
rightPanel.add(cList);
}
else
{
rightPanel.removeAll();
rightPanel.add(new Label("ITEM COMPONENT"));
CompList cList = new CompList("Hardware");
rightPanel.add(cList);
}
}
} */

Also, the program that i run to create the Frame is just this:
/* Program to create a window */
import java.awt.*;
import javax.swing.*;
public class POCreate
{
public static void main(String[] args)
{
POFrame pof = new POFrame();
}
}
 
shree vijay
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
The CompList class in the above example is as follows:
I forgot to add that before:

import java.awt.*;

public class CompList extends List
{
String[] hComps = {"RAM","CDROM","MOTHERBOARD"};
String[] sComps = {"Windows","Weblogic","Flash"};
public CompList(String type)
{
super(3,false);

if (type.equals("Hardware"))
{
for (int i=0;i<hComps.length;i++)>
{
add(hComps[i]);
}
}
else
{
for (int i=0;i<sComps.length;i++)>
{
add(sComps[i]);
}
}
}
}
 
shree vijay
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
The CompList class in the above example is as follows:
I forgot to add that before:

import java.awt.*;

public class CompList extends List
{
String[] hComps = {"RAM","CDROM","MOTHERBOARD"};
String[] sComps = {"Windows","Weblogic","Flash"};
public CompList(String type)
{
super(3,false);

if (type.equals("Hardware"))
{
for (int i=0;i<hComps.length;i++)>
{
add(hComps[i]);
}
}
else
{
for (int i=0;i<sComps.length;i++)>
{
add(sComps[i]);
}
}
}
}
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


im not sure how much this will help. I have a Choice in my Applet named picts.
public class Farm extends java.applet.Applet implements ItemListener {
picts.addItemListener(this); //makes the applet the listener
//the method of the applet that handles the event
synchronized public void itemStateChanged(final java.awt.event.ItemEvent p1) {
selection = picts.getSelectedIndex();
Image im = this.getImage(this.getDocumentBase(),picts.getSelectedItem());
an.updateImage(im);
}


[This message has been edited by Randall Twede (edited November 29, 2000).]
[This message has been edited by Randall Twede (edited November 29, 2000).]
 
When it is used for evil, then watch out! When it is used for good, then things are much nicer. Like this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic