aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes JSlider update another JSlider problems Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JSlider update another JSlider problems" Watch "JSlider update another JSlider problems" New topic
Author

JSlider update another JSlider problems

Bill Zdarko
Greenhorn

Joined: Aug 11, 2012
Posts: 5
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.

Im stuck.

Thanks

-Bill

Here is my code.

Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
this will fix your JSlider sync problems, I'll leave it to you to 'load' the textfield values.

Kocha Sapam
Ranch Hand

Joined: Aug 04, 2012
Posts: 35
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 ?
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4542
    
    5

An alternative to removing and re-adding the ChangeListener is to set and test the valueIsAdjusting property.


luck, db
There are no new questions, but there may be new answers.
Bill Zdarko
Greenhorn

Joined: Aug 11, 2012
Posts: 5
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.

Thanks again.

-Bill
Bill Zdarko
Greenhorn

Joined: Aug 11, 2012
Posts: 5
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
non-primitive objects.

Thanks again.

-Bill
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4542
    
    5

Bill Zdarko wrote:I did not think that it was possible to compare with == to
non-primitive objects.

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.
Kocha Sapam
Ranch Hand

Joined: Aug 04, 2012
Posts: 35
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

Regards - Kocha
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: JSlider update another JSlider problems