Why is itemStateChanged on JComboBox is called twice when changed?

Have a look at this source: import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Tester { public Tester(){ JComboBox box = new JComboBox(); box.addItem(“One”); box.addItem(“Two”); box.addItem(“Three”); box.addItem(“Four”); box.addItemListener(new ItemListener(){ public void itemStateChanged(ItemEvent e){ System.out.println(e.getItem() + ” ” + e.getStateChange() ); } }); JFrame frame = new JFrame(); frame.getContentPane().add(box); frame.pack(); frame.setVisible(true); } public static void main(String … Read more

JComboBox Selection Change Listener?

It should respond to ActionListeners, like this: combo.addActionListener (new ActionListener () { public void actionPerformed(ActionEvent e) { doSomething(); } }); @John Calsbeek rightly points out that addItemListener() will work, too. You may get 2 ItemEvents, though, one for the deselection of the previously selected item, and another for the selection of the new item. Just … Read more