Link to home
Start Free TrialLog in
Avatar of johnome
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.
Avatar of bobbit31
bobbit31
Flag of United States of America image

what's the problem? something like this should work:

ItemListener il = new ItemListener() {
   public void itemStateChanged(ItemEvent e)
   {
        if(e.getSource() instanceof Choice)
        {
             userChoice = track.getSelectedItem();
             repaint();
        }
   }

};

JComboBox track = new JComboBox();
track.addItemListener(il);
Avatar of johnome
johnome

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
Avatar of bobbit31
bobbit31
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of CEHJ
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);
     }    
}


Avatar of johnome

ASKER

thanks very much bobbit
> thanks very much bobbit
no problem, but why the 'B' grade?
Grade changed per request ...

** Mindphaser - Community Support Moderator **