Solved

getting a JSlider to not go over a certain value

Posted on 2004-08-23
15
578 Views
Last Modified: 2008-01-16
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.
0
Comment
Question by:Dawkins
  • 5
  • 4
  • 2
  • +2
15 Comments
 
LVL 9

Expert Comment

by:DrWarezz
Comment Utility
perhaps try this:

repaint();

Good luck,
[r.D]
0
 
LVL 9

Expert Comment

by:DrWarezz
Comment Utility
So:

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

Author Comment

by:Dawkins
Comment Utility
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
 
LVL 92

Expert Comment

by:objects
Comment Utility
> 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
 

Author Comment

by:Dawkins
Comment Utility
>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
 

Author Comment

by:Dawkins
Comment Utility
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
 
LVL 92

Expert Comment

by:objects
Comment Utility
Why don't you set the maximum value for the slider?
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 35

Expert Comment

by:girionis
Comment Utility
>  //slider.updateUI(); // This works but seems a real hack - and it causes crashes

What crashes does it cause?
0
 

Author Comment

by:Dawkins
Comment Utility
> 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
 
LVL 35

Expert Comment

by:girionis
Comment Utility
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
 
LVL 1

Accepted Solution

by:
SlimHealer earned 125 total points
Comment Utility
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
 
LVL 92

Expert Comment

by:objects
Comment Utility
Use a custom BoundedRangeModel to limit the value as required.
0
 
LVL 1

Expert Comment

by:SlimHealer
Comment Utility
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
 
LVL 92

Expert Comment

by:objects
Comment Utility
If I get some time I'll see what I can come up with.
0
 

Author Comment

by:Dawkins
Comment Utility
Thanks SlimHealer that seems to do the trick
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now