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?
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.
Joined: Jun 27, 2000
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.