4 Ways to Create a JSlider - Swing

Screenshot of JSlider Example
Here are 4 simple ways to create a JSlider in Swing. The JSlider class is big, there is no doubt in that. It has got about 6 constructors (here I'll explain 5 of them which I'll list here).


JSlider(int orientation)
JSlider(int min,int max)
JSlider(int min,int max,int val)
JSlider(int orientation,int min,int max,int val)


public void setExtent(int extent) // value+extent is maximum value to which a jslider can be set to. See constraints below.

public void setFont(Font f) // font for labels

public void setInverted(boolean flag) // slide from the inverted direction

public void setLabelTable(Dictionary d) // labels dictionary containing value-label pairs

public void setMajorTickSpacing(int space) // value between big ticks

public void setMinorTickSpacing(int space) // value between small ticks

public void setMinimum(int min) // minimum value for jslider

public void setMaximum(int max) // maximum value for jslider

public void setOrientation(int orientation) // Set orientation horizontal/vertical

public void setSnapToTicks(boolean b) // Auto adjust JSlider to nearest tick when value is changed

public void setPaintLabels(boolean b) // Paint labels

public void setPaintTicks(boolean b) // Paint tick (a tick is a line that corresponds a value)

Constraints: You need to know the constraints to understand what an extent means.

minimum <= value <= value+extent <= maximum

import javax.swing.*;
import java.awt.*;
import java.util.*;
class JSliderExample extends JFrame
JSlider s1,s2,s3,s4,s5;
Hashtable<Integer,JLabel> h=new Hashtable<>();
    h.put(0,new JLabel("Zero"));
    h.put(10,new JLabel("Ten"));
    h.put(20,new JLabel("Twenty"));
    h.put(30,new JLabel("Thirty"));
    h.put(40,new JLabel("Forty"));
    h.put(50,new JLabel("Fifty"));
    // This is not taken, see down since
    // s4 maximum val is 50
    // h.put(60,new JLabel("Sixty"));

    public JSliderExample()
    private void createAndShowGUI()
        setTitle("JSlider Example");
        setLayout(new FlowLayout());
        s1=new JSlider();
        // orientation
        s2=new JSlider(SwingConstants.VERTICAL);
        // min,max
        s3=new JSlider(10,90);
        // min,max,val
        s4=new JSlider(0,50,10);
        // orientation, min,max,val
        s5=new JSlider(SwingConstants.HORIZONTAL,0,100,20);
        // Set value
        // Set minimum,maximum
        // If you set more than maximum, then
        // maximum is considered, i.e.
        // here setValue(40) is substituted
        System.out.println("Value of s1 is "+s1.getValue());
        // Set orientation
        // Set border
        // Set paint labels
        s1.setFont(new Font("Tahoma",Font.PLAIN,11));
        // This doesn't work because, in label table, separate
        // JLabel objects are acting as JLabels
        // s4.setFont(new Font("Tahoma",Font.PLAIN,11));
        // Now though the max is 100, user can
        // drag slider upto 100-20=80 only
        // i.e. (max-setExtent)=max_val
    public static void main(String args[])
        new JSliderExample();

Also see combining JSlider and JMenu and increase JTextArea font size with JSlider

The greatest compliment you can give me is when you share this with others. I sincerely appreciate it :)

Posted by at on

Tags: Swing Components,