Search code examples
javaswingjslider

JSliders only sliding halfway


I have a number JSliders in my application and I'm wondering as to why they only slide to half way.

enter image description here

public class test1 extends javax.swing.JFrame {
    public test1() {
        initComponents();
        jSlider1.setExtent(255);
        jSlider1.setValue(-255);
}
@SuppressWarnings("unchecked")                     
private void initComponents() {

    jSlider1 = new javax.swing.JSlider();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

}                    

public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new test().setVisible(true);
        }
    });
}

private javax.swing.JSlider jSlider1;                
}

Solution

  • Using setExtent or setMaximum would seem to work the same but setExtent makes it so that the slider only slides half way. The proper code looks like this:

    public class test1 extends javax.swing.JFrame {
        public test1() {
            initComponents();
            jSlider1.setMaximum(255);
            jSlider1.setValue(-255);
    }
    @SuppressWarnings("unchecked")                     
    private void initComponents() {
    
        jSlider1 = new javax.swing.JSlider();
    
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
    }                    
    
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new test().setVisible(true);
            }
        });
    }
    
    private javax.swing.JSlider jSlider1;                
    }