Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JSlider update another JSlider problems

 
Bill Zdarko
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this will fix your JSlider sync problems, I'll leave it to you to 'load' the textfield values.

 
Kocha Sapam
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5126
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An alternative to removing and re-adding the ChangeListener is to set and test the valueIsAdjusting property.
 
Bill Zdarko
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5126
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic