permaculture playing cards*
The moose likes Swing / AWT / SWT and the fly likes JSpinner sending multiple events Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JSpinner sending multiple events" Watch "JSpinner sending multiple events" New topic
Author

JSpinner sending multiple events

Chris Shepherd
Ranch Hand

Joined: Jun 27, 2000
Posts: 286
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

Joined: Oct 27, 2005
Posts: 19655
    
  18

Movig to Swing


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> 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

Joined: Oct 27, 2005
Posts: 19655
    
  18

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

Joined: Jun 27, 2000
Posts: 286
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JSpinner sending multiple events
 
Similar Threads
destroy() does not stop Process object
assignment problem
4b structure
AJAX Issue - Messages got lost...
Problem calling piped perl routine