• Post Reply Bookmark Topic Watch Topic
  • New Topic

itemStateChanged is fired TWICE on JCombBox?

 
verduka fox
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Verduka,
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,
Manfred.
 
verduka fox
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Manfred,
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!
 
Takeshi Toyohara
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Verduka,
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 ]
 
Sajee Joseph
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!