johnome
asked on
Help: JComboBoxes and Event handling
Hi,
I'm trying to create a JApplet that has several JComboBoxes. I have it designed but don't know how to get event handling to work. Does anyone know the JApplet equivalant of the following as this works with normal applets
public void itemStateChanged(ItemEvent e)
{
if(e.getSource() instanceof Choice)
{
userChoice = track.getSelectedItem();
repaint();
}
}
note that track is just the name of a JComboBox.
Thanks for your time.
I'm trying to create a JApplet that has several JComboBoxes. I have it designed but don't know how to get event handling to work. Does anyone know the JApplet equivalant of the following as this works with normal applets
public void itemStateChanged(ItemEvent
{
if(e.getSource() instanceof Choice)
{
userChoice = track.getSelectedItem();
repaint();
}
}
note that track is just the name of a JComboBox.
Thanks for your time.
ASKER
That works for Choices on normal applets but not on JComboBoxes for JApplets. It;s the instanceof Choice that's the problem. Do i use casting of some type?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Not quite sure what the difficulty is here. The only thing you should watch is that deselection events occur as well.
public class A extends JApplet implements ItemListener {
public void init() {
Container contentPane =getContentPane();
try {
Vector items = new Vector(4);
items.add("1");
items.add("2");
items.add("3");
items.add("4");
DefaultComboBoxModel model = new DefaultComboBoxModel(items );
JComboBox combo = new JComboBox(model);
contentPane.add(combo, BorderLayout.NORTH);
combo.addItemListener(this );
}
catch(Exception e){
e.printStackTrace();
}
}
public void itemStateChanged(ItemEvent e) {
System.out.println(e);
}
}
public class A extends JApplet implements ItemListener {
public void init() {
Container contentPane =getContentPane();
try {
Vector items = new Vector(4);
items.add("1");
items.add("2");
items.add("3");
items.add("4");
DefaultComboBoxModel model = new DefaultComboBoxModel(items
JComboBox combo = new JComboBox(model);
contentPane.add(combo, BorderLayout.NORTH);
combo.addItemListener(this
}
catch(Exception e){
e.printStackTrace();
}
}
public void itemStateChanged(ItemEvent
System.out.println(e);
}
}
ASKER
thanks very much bobbit
> thanks very much bobbit
no problem, but why the 'B' grade?
no problem, but why the 'B' grade?
Grade changed per request ...
** Mindphaser - Community Support Moderator **
** Mindphaser - Community Support Moderator **
ItemListener il = new ItemListener() {
public void itemStateChanged(ItemEvent
{
if(e.getSource() instanceof Choice)
{
userChoice = track.getSelectedItem();
repaint();
}
}
};
JComboBox track = new JComboBox();
track.addItemListener(il);