jQuery in Action
[Logo] JavaRanch » JavaRanch Saloon
  Search | FAQ | Recent Topics | Hot Topics
Register / Login


Win a copy of Flex 4 in Action this week in the Flex forum!
Reply Bookmark it! Watch this topic JavaRanch » Forums » Java » Swing / AWT / SWT / JFace
 
RSS feed
 
New topic
Author

JSpinner sending multiple events

Chris Shepherd
Ranch Hand

Joined: Jun 27, 2000
Messages: 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 Prime
Bartender

Joined: Oct 27, 2005
Messages: 8776

Movig to Swing

SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Michael Dunn
Rancher

Joined: Jun 09, 2003
Messages: 3090

> 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 Prime
Bartender

Joined: Oct 27, 2005
Messages: 8776

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.

SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Chris Shepherd
Ranch Hand

Joined: Jun 27, 2000
Messages: 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
 
 
 
Reply Bookmark it! Watch this topic JavaRanch » Forums » Java » Swing / AWT / SWT / JFace
 
RSS feed
 
New topic