Search code examples
javajlabeljprogressbarjslider

Java Slider and ProgressBar


I have two minor problems with a ProgressBar that I have connected to a Slider in a Java Frame I am working on. I am using NetBeans. I have added this code :

 private void jSlider1StateChanged(javax.swing.event.ChangeEvent evt) {              
 JSlider source = (JSlider)evt.getSource() ;
 int value = source.getValue() ;
 jProgressBar1.setValue(value) ;}   
  1. As I move the slider, the ProgressBar moves with the movement of the Slider. However, it doesn't show any value in the Slider. I think with the code above I am setting to it do this, but it just moves and no value is printed in the progressBar. What is missing please?

  2. This should be a really stupid simple question. The labels I have got under my Slider, is shown when i set the min/max from 0 - 5. When I set the min/max to 1 - 5, the label 1 is shown but 5 is NOT shown. What should I to see Labels 1 - 5 under a slider?


Solution

  • For your first problem, you should set the progressBar.setStringPainted(true); on your progressbar, to show specific values in the progressbar add the following line:

    jProgressBar1.setValue(value); 
    jProgressBar1.setString(String.valueOf(value)); // this one 
    

    For more information check the documentation of the JProgressBar.