JComboBox fires selection ActionEvent when I initialize it and I don't want it to.
Hi, I am comfortable using JComboBox. I am now using an ActionListener to catch selection change ActionEvents and do something with them in the actionPerformed() that handles the event (get the selected index, get the element at the selected index in an array of objects, and display its values in a JOptionPane -will be displayed in components when I get it working). The problem is JComboBox.addItem() fires a selection change event when I am initializing the combo. When this happens, the selected index is either 0 (the first item is selected) or -1 (invalid). So while the combo is being initializing the combo and before the GUI displays, the program either displays the first item or crashes because the index is invalid. There is a similar problem with dynamically adding or deleting an item (JComboBox.removeItemAt()). I can fix this by not adding the ActionListener until the combo is initialized and removing the ActionListener before an add or delete and (re)adding it after the add or delete, but isn't there a better way?
Thanks.
Re: JComboBox fires selection ActionEvent when I initialize it and I don't want it to
For a listener to ignore programmatic changes, I usually use a boolean flag.
db