Java Swing Slider Example

In this post, I show you how to create a slider using the JSlider component in swing-based applications.
Check out complete Swing tutorial at https://www.javaguides.net/p/java-swing-tutorial.html.
JSlider is a component that lets the user graphically select a value by sliding a knob within a bounded interval. Moving the slider's knob, the stateChanged() method of the slider's ChangeListener is called.
Tick marks - JSlider can optionally show tick marks for the range of its values. The tick marks are controlled with the setMinorTickSpacing(), setMajorTickSpacing(), and setPaintTicks() methods.

Java Swing Slider Example

In below code example, a value selected from a slider is displayed in a label component:
import javax.swing.GroupLayout;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import java.awt.EventQueue;

public class SliderExample extends JFrame {

    private static final long serialVersionUID = 1 L;
    private JSlider slider;
    private JLabel lbl;

    private void initializeUI() {

        slider = new JSlider(0, 100, 0);
        slider.setMinorTickSpacing(5);
        slider.setMajorTickSpacing(10);
        slider.setPaintTicks(true);

        slider.addChangeListener((ChangeEvent event) - > {

            int value = slider.getValue();
            lbl.setText(Integer.toString(value));
        });

        lbl = new JLabel("...");

        createLayout(slider, lbl);

        setSize(300, 150);
        setTitle("JSlider");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
    }

    private void createLayout(JComponent...arg) {

        JPanel pane = (JPanel) getContentPane();
        GroupLayout gl = new GroupLayout(pane);
        pane.setLayout(gl);

        gl.setAutoCreateContainerGaps(true);
        gl.setAutoCreateGaps(true);

        gl.setHorizontalGroup(gl.createParallelGroup()
            .addComponent(arg[0])
            .addComponent(arg[1])
        );

        gl.setVerticalGroup(gl.createSequentialGroup()
            .addComponent(arg[0])
            .addComponent(arg[1])
        );

        pack();
    }

    public static void main(String[] args) {

        EventQueue.invokeLater(() - > {

            SliderExample sliderExample = new SliderExample();
            sliderExample.initializeUI();
            sliderExample.setVisible(true);
        });
    }
}
Let's understand above Java program.
A JSlider is created with the minimum, maximum, and current values are parameters:
slider = new JSlider(0, 100, 0);
We set the distances between minor a major tick marks:
slider.setMinorTickSpacing(5);
slider.setMajorTickSpacing(10);
The setPaintTicks() method determines whether tick marks are painted on the slider:
slider.setPaintTicks(true);

Output

Check out complete Swing tutorial at https://www.javaguides.net/p/java-swing-tutorial.html.

Comments