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.
DawkinsAsked:
Who is Participating?
 
SlimHealerCommented:
There are a couple of things going on here.

1) As a result of the call to setValue(), you end up firing a new event from inside the handler to an event -- generally this causes problems.  Using invokeLater() fixes this

2) You are way too many events.  Using getValueIsChanging() addresses this:

Here's a re-worked version of your example without the updateUI() call:


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

public class TestCase implements ChangeListener{
 int maximumValue = 60;
      
 public TestCase() {
 }
 
 public void stateChanged(ChangeEvent e) {
  final JSlider slider = (JSlider) e.getSource();
  int value = slider.getValue();
  System.out.println("User setting value of: " + value);
  if ((value > maximumValue) && !slider.getValueIsAdjusting() ) {
        javax.swing.SwingUtilities.invokeLater( new Runnable() {
       public void run() {
         System.out.println("Value is greater than 60!       I'd like it to stop now!");
            slider.setValue(maximumValue);
       }
    });        
   }
  }

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




0
 
DrWarezzCommented:
perhaps try this:

repaint();

Good luck,
[r.D]
0
 
DrWarezzCommented:
So:

if (value > parentGUI.getMaxPopulation()) {
        value = parentGUI.getMaxPopulation();
        slider.setValue(value);
        repaint();
}
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
DawkinsAuthor Commented:
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.
0
 
objectsCommented:
> 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.
0
 
DawkinsAuthor Commented:
>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();
   }
}
0
 
DawkinsAuthor Commented:
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.
0
 
objectsCommented:
Why don't you set the maximum value for the slider?
0
 
girionisCommented:
>  //slider.updateUI(); // This works but seems a real hack - and it causes crashes

What crashes does it cause?
0
 
DawkinsAuthor Commented:
> 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.
0
 
girionisCommented:
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)?
0
 
objectsCommented:
Use a custom BoundedRangeModel to limit the value as required.
0
 
SlimHealerCommented:
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.
0
 
objectsCommented:
If I get some time I'll see what I can come up with.
0
 
DawkinsAuthor Commented:
Thanks SlimHealer that seems to do the trick
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.