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.

Related Posts Plugin for WordPress, Blogger...

Leave a Reply

Your email address will not be published. Required fields are marked *