dcsimg

Why is itemStateChanged() fired twice when I select an item in a JComboBox?

Jesper Berglund

The ItemStateChanged event comes twice because the it is one deselect event and one select event.

To check if it is deselected or selected one might do like this in the itemStateChanged(ItemEvent e) method:


public void itemStateChanged(ItemEvent e) {
  if (e.getStateChange() == e.SELECTED) {
    //Do the thing when something is selected
  }

  else if(e.getStateChange() == e.DESELECTED) {
    //Do the thing when something is deselected
  }
}