Forums Register Login
itemStateChanged is fired TWICE on JCombBox?
I have a JComboBox in a JApplet. When the user selects a value in the combo box, the itemStateChanged event is firing twice. Why is this? I'm assuming it is getting fired once when the user selects the value in the combo box, and again when the combo box is collapsed. Does anyone know why this is happening? Do you know how to prevent it from happening?
Thanks in advance!
I have noticed that this happens with JList also. The two events are generated for mouse down and then for mouse up. For lists the following works. Use the boolean event method isAdjusting() for the first callback it should be true and for the second is should be false. Therefore if you only want the result then make sure the adjusting flag is false.

Good Luck,
Your idea sounds like what I'm looking for. However, I don't see the isAdjusting() method off of ItemEvent. I tried compiling and get the following error:

Can you please clarify? Thanks!
i have run into the same problem before and there are two solutions i found.
One was to add an ActionListener to the jcombobox instead of a itemlistener, which created the result of firing off only 1 event every time something was selected(or reselected) in the comboboxlist.
the second was to use:
if (event.getStateChange() == ItemEvent.SELECTED)
in the itemStateChanged() method.
in this case, this code gets run once ONLY when the selected item actually changes.
[ February 03, 2002: Message edited by: Takeshi Toyohara ]
If u r using the itemStateChanged() event then it will be fired twice when u select an item. However this is not a bug. It is fired first when the curently selected item is changed. and it is fired next whne a new item is selected (quite logical)
It is no measure of health to be well adjusted to a profoundly sick society. -Krishnamurti Tiny ad:
Why should you try IntelliJ IDEA ?

This thread has been viewed 3512 times.

All times above are in ranch (not your local) time.
The current ranch time is
Aug 14, 2018 14:23:10.