Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Listener Called Multiple Times on Single Click  RSS feed

 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following listener on a tableset of names:

The println shows that getValueIsAdjusting is called 4 times when I click a single selection from the list. Is this normal behavior? If so how do I determine which call to really use. If not, where can I look to see why it is being called 4 times on a single click? TIA.
 
Tony Docherty
Bartender
Posts: 3264
81
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ListSelectionListener gets called multiple times so you can keep track of things like multiple selections etc. If you just want to handle the event once all activity has completed negate your test for value adjusting ie use
 
Rob Camick
Ranch Hand
Posts: 2787
12
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does the clearSelection(...) cause the listener to be invoked again?
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply and I well give that a try. However, I need to better understand what each call represents. I don't see what the four calls mean or how to determine each. I can see maybe 2 calls, mouse button down and mouse button up but I don't see the other 2 and I don't see how it knows when it is done for multiple selections.
 
Paul Clapham
Sheriff
Posts: 22374
42
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Normally I would expect the listener to be called twice when you select a different element in a JList -- that's assuming you're using single selection and not some form of multiple selection. For example if A was selected and then you click on B to select that instead, then you get one call which says "A was deselected" and a second call which says "B was selected". The getValueIsAdjusting() trick which was already mentioned earlier allows you to ignore the first one, which is usually what you want to do.

So if you're getting four calls, that leads me to suspect that you added the listener to the list twice.
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. That makes more sense. I'll have to see if/how I got it in twice.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe you added a selection listener to both the row selection model and the column selection model?
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After a bunch of debug output, I think I understand what is happening but I don't know how to fix it. When a row is selected (single mode) I process that selection and then remove it from the list so it cannot be selected again (line 12). At that point I think another event is triggered with the same row. But since it was deleted is now has a different row value (e.getFirstRowIndex != previousRow) so it gets reprocessed as an additional selection. Maybe I need to swap lines 12 & 13? Thanks.
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I figured this out, perhaps not the best way but it works. I set a flag before the 'deleteRow' call and added that to the previous 'if' block so that when that flag is set, it does not process the event. I then reset the flag after the 'deleteRow'.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!