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
  }
}
0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact