• 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

event firing two times in valuChanged()

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
i am selecting an item from JList.if i select it calls valuChanged method from ListSelectionEvent .but the event
is fired two times.
why its happenings.
plz let me know
thanx in advance
 
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The same thing must be happening (as far as I can tell) as in this example: http://www.javaranch.com/ubb/Forum2/HTML/001047.html
Notice my comments in the code. In the case of a combobox, you can use (ItemEvent e.getStateChange()) to determine if the change is due to a de-selection, or a selection. But the ListSelectionEvent does not have getStateChange(). I'm not clear from the API how you would determine which of the two events being raised is for which event (the deselect of on JList item, or the subsequent select of the new item). Also, the API is pretty silent on the whole "I'm going to throw two of these for every change"!! Grr.
Anyone have any ideas?
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it.
Java has made a total mess (IMNSHO) of this type of thing.
Can we all agree that a JList, which is a list of items, and a JComboBox (which is another sort of list), should generally behave the same way in terms of selection events that they throw?
Well anyways..
JComboBox - You can add an ItemListener, which requires you to define behaviour for when itemStateChange is called. This method is passed an ItemEvent, which you can then query as to its getStateChange() to determine whether the event is a lost-focus or gained-focus type of event. Or - I haven't tested it, but you can also add an ActionListener, which requires you to define behaviour for actionPerformed, which it would *hopefully* call only once (when you click the combobox?).

JList - You cannot use either ItemListener or ActionListener, but must use instead ListSelectionListener. This requires you to define behaviour for valueChanged. This method is passed a ListSelectionEvent, but you cannot query this type of event as to its getStateChange() BUT. There is a getValueIsAdjusting() method you can call. If the answer is true, then most likely, the event is the first of the two events (the one where the 'previous' item is losing focus), and if it is false, it's probably the second event (the one where the 'current' item is gaining focus).
ARGH! It makes me wish for the simplicity (simple-minded?) VB!

I haven't done the "catch and release" behaviour that I did for the that other thread I mentioned in my last post. But you can see from the println() what you need to check.

BTW: This code is umm... maybe changed enough to break the copyright of it's original writers.
[This message has been edited by Mike Curwen (edited March 29, 2001).]
 
For my next feat, I will require a volunteer from the audience! Perhaps this tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic