Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 213
  • Last Modified:

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.
0
johnome
Asked:
johnome
1 Solution
 
bobbit31Commented:
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);
0
 
johnomeAuthor Commented:
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?
0
 
bobbit31Commented:
why not:
 if(e.getSource() instanceof JComboBox)
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
CEHJCommented:
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);
     }    
}


0
 
johnomeAuthor Commented:
thanks very much bobbit
0
 
bobbit31Commented:
> thanks very much bobbit
no problem, but why the 'B' grade?
0
 
MindphaserCommented:
Grade changed per request ...

** Mindphaser - Community Support Moderator **
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now