Meaningless Drivel is fun!*
The moose likes Swing / AWT / SWT and the fly likes event firing two times in valuChanged() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "event firing two times in valuChanged()" Watch "event firing two times in valuChanged()" New topic

event firing two times in valuChanged()

wip siva
Ranch Hand

Joined: Nov 03, 2000
Posts: 36
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
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

The same thing must be happening (as far as I can tell) as in this example:
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

Joined: Feb 20, 2001
Posts: 3695

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).]
I agree. Here's the link:
subject: event firing two times in valuChanged()
Similar Threads
calling event from another.
Loss of context variables in Seam
Starting a new frame with JComboBox?
JTABLE's valuchanged method?
Parameter Passing