Class JSlider

extended by java.awt.Component
 extended by java.awt.Container
     extended by javax.swing.JComponent
         extended by javax.swing.JSlider
All Implemented Interfaces:
ImageObserver, MenuContainer, Serializable, Accessible, SwingConstants


public class JSlider
extends JComponent
implements SwingConstants, javax.accessibility.Accessible, java.awt.image.ImageObserver, java.awt.MenuContainer, java.io.Serializable

A visual component that allows selection of a value within a range by adjusting a thumb in a track. The values for the minimum, maximum, extent and value are stored in a DefaultBoundedRangeModel.

A JSlider component has the following properties:

PropertyStored inBound?

The various behavioural aspects of these properties follows:

  • When a non-bound property stored in the slider changes, the slider fires a javax.swing.event.ChangeEvent to its change listeners.
  • When a bound property stored in the slider changes, the slider fires a java.beans.PropertyChangeEvent to its property change listeners.
  • If any of the model’s properties change, it fires a javax.swing.event.ChangeEvent to its listeners, which include the slider.
  • If the slider receives a javax.swing.event.ChangeEvent from its model, it will propagate the event to its own change listeners, with the event’s “source” property set to refer to the slider, rather than the model.


1. JSlider Basics

In the AWT, the Scrollbar class played double duty as a control for interactively selecting numeric values and a widget used to control scrolling. This was inconvenient, and resulted in poor looking sliders. Swing gives you a “real” slider: JSlider. You create a JSlider in a similar manner to Scrollbar: the zero-argument constructor creates a horizontal slider with a range from 0 to 100 and an initial value of 50. You can also supply the orientation (via JSlider.HORIZONTAL or JSlider.VERTICAL) and the range and initial value to the constructor. You handle events by attaching a ChangeListener. Its stateChanged method normally calls getValue to look up the current JSlider value.

2. New Features: Tick Marks and Labels

Swing sliders can have major and minor tick marks. Turn them on via setPaintTicks(true), then specify the tick spacing via setMajorTickSpacing and setMinorTickSpacing. If you want users to only be able to choose values that are at the tick marks, call setSnapToTicks(true). Turn on labels via setPaintLabels(true). This draws labels for the major tick marks. You can also specify arbitrary labels (including ImageIcons or other components) by creating a Dictionary with Integers as keys and Components as values, then associating that with the slider via setLabelTable.

Other capabilities include: borders (as with all JComponents), sliders that go from high to low instead of low to high (setInverted(true)), and the ability to determine that you are in the middle of a drag (when getValueIsAdjusting() returns true) so that you can postpone action until the drag finishes.

3. JSlider Example: Source Code

Following are three simple sliders. The first is a plain one, the second has tick marks, and the third has both tick marks and labels.


import java.awt.*;
import javax.swing.*;

public class JSliders extends JFrame {
public static void main(String[] args) {
new JSliders();

public JSliders() {
super(“Using JSlider”);
// Comment out next line for Java LAF
addWindowListener(new ExitListener());
Container content = getContentPane();

JSlider slider1 = new JSlider();
slider1.setBorder(BorderFactory.createTitledBorder(“JSlider without Tick Marks”));
content.add(slider1, BorderLayout.NORTH);

JSlider slider2 = new JSlider();
slider2.setBorder(BorderFactory.createTitledBorder(“JSlider with Tick Marks”));

content.add(slider2, BorderLayout.CENTER);

JSlider slider3 = new JSlider();
slider3.setBorder(BorderFactory.createTitledBorder(“JSlider with Tick Marks & Labels”));

content.add(slider3, BorderLayout.SOUTH);


Note: also requires WindowUtilities.java and ExitListener.java, shown earlier.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.