This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Hi guys, I am taking Java classes at a college level.
I am playing around with JSliders and an addChangeListener.
The gist of my code is I have two sliders, one is MPH the other is KPH. When one slider is slid, the two fields update and the other slider follows. if you move the other slider, the same happes.
My problems seems that when the addChangeListener updates the other slider, it calls the same addChangeListener and then they both stick at 0.
I am using an if statement to run seperate commands depending on which slider calls the addChangeListener.
in this code if I uncomment the statements to update the other slider in the addChangeListener then it doesnt work, with them commented, the other slider does not update.
Yes! That's the very annoying condition. Here's the problem : if you adjust 'mph' then with some computations done (like converting mph to kph), you call the setValue(someint) method on the 'kph' slider which triggers the event listener in 'kph' which in turn again calls setValue(someint1) on the 'mph' slider after the calculations and then you know the two sliders keeps updating each other infinitely in a loop (just like a badly writen 'while' or 'for' loop). Now your app must be in a hang, aint it ?
An alternative to removing and re-adding the ChangeListener is to set and test the valueIsAdjusting property.
There are no new questions, but there may be new answers.
Joined: Aug 11, 2012
You guys are awesome. I didnt expect a solution for a few days. Im sure I will use this site alot, knowing its friendly and quick.
Joined: Aug 11, 2012
After playing with it some more, here is what I found that was changed.
then each slider was changed to sl rather than calling a new SliderListener()
this makes me think that before, each slider was using a different instance of the class SliderListener,
now they are using the same object or instance.
THe code in the SliderListener is mainly this.
the listener is turn off, a change is made, its turned back on. Nice!
I think the only thing I do not get is this,
both source and mphSlider are JSliders. I did not think that it was possible to compare with == to
Bill Zdarko wrote:I did not think that it was possible to compare with == to
That doesn't compare Objects at all. It test the equality of references.
Two references are equal if they both refer to the same Object.
Joined: Aug 04, 2012
Agree with the valueIsAdjusting case, you simply check whether current slider (mph or kph) is adjusting, if so we are safe to call setValue(someint1) on the other slider (since valueIsAdjusting is true only when you are acting on the slider with a mouse, not with setValue()) and if not you check if the other is adjusting if yes we dont call setValue(someint) on it. You get the Idea ? Maybe i could post a SSCC code later.
Edit : And one more point, use two annonymous inner classes for the ChangeListeners - its more convenient IMHO though YMMV