Question

I have 2 combo boxes. The upper combo box dynamically changes the items on the second combo box.

How can the ActionPerformed event know, that the event was triggered because the upper combo box changed the items?

I tried to I set actioncommands to both combo box. (Upper and lower). When the upper combo box changes the items on the lower combo box, i get the command lower because it changed it...

EDIT: i changed the description

Était-ce utile?

La solution

For this you should use the actionCommand:

//after object creation:
upperCombo.setActionCommand("upper");
lowerCombo.setActionCommand("lower");

And then in your actionPerformed():

String command = event.getActionCommand();
if("upper".equals(command)){
  //Do something
} else if("lower".equals(command)){
  //Do something
} 

Does this solve your problem, or did I misunderstood you?

EDIT:

What about somthing like this:

String command = event.getActionCommand();
if("upper".equals(command)){
  lowerCombo.removeActionListener(this);
  //Do your changes
  lowerCombo.addActionListener(this);
} else if("lower".equals(command)){
  //Do something
} 

EDIT (again ;)):

class YourClass{
 private ActionListener lowerActionListener = new java.awt.event.ActionListener() { 
   void actionPerformed(java.awt.event.ActionEvent evt) { 
    //doSomething 
   }
 };

 //...
 // maybe in the constructor, after object-creation
 lowerCB.addActionListener(lowerActionListener);

 //...
 // in th actionPerfomed() method
 lowerCB.removeActionListener(lowerActionListener);

}

Autres conseils

Well, the normal solution is to have two action listeners, one for each combo box.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top