Results 1 to 2 of 2
- 08-07-2012, 08:13 PM #1Member
- Join Date
- Aug 2012
- Rep Power
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?
- 08-08-2012, 05:20 AM #2
- By Vizoere in forum New To JavaReplies: 11Last Post: 04-11-2012, 05:57 PM
- By africanhacker in forum New To JavaReplies: 3Last Post: 02-06-2011, 06:08 PM
- By kerode in forum New To JavaReplies: 1Last Post: 11-23-2010, 07:26 PM
- By bahumbaba in forum AWT / SwingReplies: 2Last Post: 12-10-2009, 02:58 PM
- By Java Tip in forum Java TipReplies: 0Last Post: 03-12-2008, 12:00 AM