Very new to Java and Swing, and I have been playing with a swing gui application. It generated some code for my combo box:
comboBox.addActionListener(EventHandler.create(ActionListener.class, TestController, "changeSomething"));
and i also have this:
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
public class StudyPlanController {
private JComboBox factors;
public void changeSomething() {
JOptionPane.showMessageDialog(null, "change!");
}
}
I have 2 questions.
-
When I change the selected item in the combo box i get the message “change!” only the first time it is changed. Why is this?
-
When googling for a solution, all the code for setting up the listener was different to the code generated for me. e.g
box.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent e){
System.out.println(e.getItem() + ” ” + e.getStateChange() );
}
});
Is the way i’m createing the listener correct? why are there two ways to do this?
Thanks
For JComboBox is better implements ItemListener, but this Listener is always called twice
SELECTEDandDESELECTED, you can check that if event isSELECTED/DESELECTEDYou can use ActionListener, but I suggest to use that for changing itself own
JComboBox'sproperties or methods, not to ouside fromJComboBox, to ouside somewhere to the GUIYou can use EventHandler, but better would be start to leaning basic stuff before