Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JSpinner sending multiple events

 
Chris Shepherd
Ranch Hand
Posts: 286
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a series of JSpinners that I am using in a GUI. I also have an inline change listener for each of them to handle the input, check it for out-of-range and pass the new value along to my software. The problem is that every second or third click on the spinner's up and down arrows generates 2 calls to stateChanged(). This forces my code to run twice, and since it can be a time consuming process when it runs, I am essentially doubling my run time for this process. I thought I could be clever and just trap the second call that occurred less that 50ms from the first one. That worked great! .... Except I realized that the second call I trapped actually carries the new value and the first call getting through carried the old one.

So my question is this: Why am I having to jump through hoops to get data off my spinners? Why is it sometimes tossing an extra call before it sends the real one I am expecting?

Thanks for any help...
Chris
 
Rob Spoor
Sheriff
Pie
Posts: 20550
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Movig to Swing
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> So my question is this: Why am I having to jump through hoops to get data off my spinners? Why is it sometimes tossing an extra call before it sends the real one I am expecting?

the answer is in your custom code:
"I thought I could be clever and just trap the second call that occurred less that 50ms from the first one."


it works OK for me in this

 
Rob Spoor
Sheriff
Pie
Posts: 20550
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could store the last spinner value, and only call your expensive code if the value has actually changed.
You can make this a method local class if needed.
 
Chris Shepherd
Ranch Hand
Posts: 286
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Michael and Rob for your reply. I actually realized I could use Rob's solution after I posted this yesterday. I keep these values stored in some of my saved objects and so I just check against the last value I had stored and ignore it if it is the same. Michael, I actually thought about your solution first yesterday, but I was too bothered by the fact that the spinner generated inconsistent results to want to develop it into a solution. It took me enough time just to chase down why my results were not what I expected. Of course, seeing it in code looks fairly straight-forward and wouldn't have been that hard to do.

Thanks to both of you for your input. Still not sure why I'm sometimes getting two events, but I have a way around it now.

Chris
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic