Set Item In JComboBox Without Affecting ItemStateChanged

Selecting a JComboBox item programmatically is pretty simple. Call either setSelectedIndex() or setSelectedItem() and that’s it. Problem is though, if you added an ItemListener to the JComboBox object, every time you do a select programmatically, the ItemStateChanged event method also gets affected.

Calling those two select methods from a JComboBox object is the most prone thing that developers do. However, if you do not want the ItemStateChanged method to be affected when you select an item programmatically, you can create a dummy variable inside the ItemStateChanged method so that it would not get affected. Sad to say, that is the alternative aside from adding and removing the ItemListener.

Donations appreciated. Every little $ helps. Or click Google +1. Or play the short video on the upper right of this page.

Related Posts Plugin for WordPress, Blogger...

Post a Comment