• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

itemStateChanged is fired TWICE on JCombBox?

 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic