Link to home
Start Free TrialLog in
Avatar of Dawkins
Dawkins

asked on

getting a JSlider to not go over a certain value

Hi all,

I would like my JSlider to "snap back"  (or just stop dead) when it goes above a certain value.  I can make its value do this, but I want the actual slider UI to stop so the user can see he has reached the max value.

Here is the code so far:

if (value > parentGUI.getMaxPopulation()) {
        value = parentGUI.getMaxPopulation();
        slider.setValue(value);
}

With this code, the slider just keeps on going up as the user slides it, even though the value doesn't.  The only way I have found to stop it is to call slider.updateUI().   However it seems this is a misuse of the updateUI function and it actually can cause a crash, so I need another way.

Just for background as to why I want to do this:  the slider is part of a group of JSliders each of which can be "locked".  The total value of all the JSliders mustn't go above the maximum value, so if one is locked the others have a new maximum value.
Avatar of DrWarezz
DrWarezz

perhaps try this:

repaint();

Good luck,
[r.D]
So:

if (value > parentGUI.getMaxPopulation()) {
        value = parentGUI.getMaxPopulation();
        slider.setValue(value);
        repaint();
}
Avatar of Dawkins

ASKER

I tried repaint and also all these! ...

        slider.revalidate();
        slider.doLayout();
        slider.invalidate();
        slider.validate();

None stop the slider from sliding above the max value.
Avatar of Mick Barry
> With this code, the slider just keeps on going up as the user slides it, even though the value doesn't.

Can you post a small compilable example that demonstrates the problem.
Avatar of Dawkins

ASKER

>Can you post a small compilable example that demonstrates the problem.

Yep, this should illustrate the problem:

----------------------------------------------------

import javax.swing.JFrame;
import javax.swing.JSlider;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;

public class SliderExample implements ChangeListener{
 
  int maximumValue = 60;
 
  public SliderExample() {
  }
 
  public void stateChanged(ChangeEvent e) {
    JSlider slider = (JSlider) e.getSource();
    int value = slider.getValue();
    System.out.println("User setting value of: " + value);
    if (value > maximumValue) {
      System.out.println("Value is greater than 60!  I'd like it to stop now!");
      slider.setValue(maximumValue);
      //slider.updateUI(); // This works but seems a real hack - and it causes crashes
    }
  }

   public static void main(String args[]) {
     SliderExample sliderExample = new SliderExample();
     JFrame frame = new JFrame();
     JSlider slider = new JSlider();
     slider.addChangeListener(sliderExample);
     frame.getContentPane().add(slider);
     frame.setSize(600,400);
     frame.show();
   }
}
Avatar of Dawkins

ASKER

Just a side note:

To see the crash caused by inserting "slider.updateUI();", increase the slider to 70 then click on the slider somewhere above the 60 mark and it throws a null pointer exception.
Why don't you set the maximum value for the slider?
>  //slider.updateUI(); // This works but seems a real hack - and it causes crashes

What crashes does it cause?
Avatar of Dawkins

ASKER

> Why don't you set the maximum value for the slider?
That makes the far right edge of the slider the maximum value, which is not what I want.  I have a series of sliders which interact with each other so that they always have the same.  If you increase one, it decreases the others.  You can also lock them, and so if you lock one at 40, then the maximum the other two can go to is now 60.

> What crashes does it cause?
In the example that I posted, if you increase the slider to 60, then click on the slider somewhere in the 60-100 range you will see the crash.  It has something to do with setting the sliders "rectangle" value to null when you call updateUI.  I found it hard to debug the SDK files because JBuilder doesn't let you see method variables in the SDK class files when you are debugging for some reason.
Yes you are right, it throws a npe. But I do not see it affects the functionallity. Maybe you could leave it like that (with the npe)?
ASKER CERTIFIED SOLUTION
Avatar of SlimHealer
SlimHealer

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Use a custom BoundedRangeModel to limit the value as required.
Could you say more, objects?  I experimented for a while with a custom BoundedRangeModel and was unable to get it to pin the UI as requested.  I'd be interested in any insight you could offer.
If I get some time I'll see what I can come up with.
Avatar of Dawkins

ASKER

Thanks SlimHealer that seems to do the trick