How to Use Sliders in Java - dummies

By Doug Lowe

Java allows you to use sliders in your programming. A slider is a component that lets a user pick a value from a set range (say, from 0 to 50) by moving a knob. A slider is a convenient way to get numeric input from the user when the input falls within a set range of values.


To create a slider control, you use the JSlider class. Check out its constructors and methods.

Constructor Description
JSlider() Creates a new slider. The minimum and maximum values default to
0 and 100, and
the initial value is set to 50.
JSlider(int min, int max) Creates a new slider with the specified minimum and maximum
values. The initial value is halfway between the minimum and
JSlider(int min, int max, int
Creates a new slider with the specified minimum, maximum, and
initial values.
JSlider(int orientation, int min, int max,
int value)
Creates a new slider with the specified minimum, maximum, and
initial values. The orientation can be JSlider.HORIZONTAL or JSlider.VERTICAL.
Method Description
void addChangeListener(ChangeListener
Adds a ChangeListener to listen for
change events.
int getValue() Gets the value indicated by the current position of the
void setFont() Sets the font of the text associated with the slider.
void setInvert(boolean value) If true, inverts the slider’s
direction so that the maximum value is on the left and the minimum
value is on the right.
void setMajorTickSpacing(int
Sets the interval for major tick marks. The marks aren’t
shown unless setPaintTicks(true) is
void setMinimum(int value) Sets the minimum value.
void setMaximum(int value) Sets the maximum value.
void setMinorTickSpacing(int
Sets the interval for minor tick marks. The marks aren’t
shown unless setPaintTicks(true) is
setOrientation(int orientation) Sets the orientation. Allowed values are JSlider.HORIZONTAL and JSlider.VERTICAL.
void setPaintLabels(boolean
If true, shows tick labels.
void setSnapToTicks(boolean
If true, rounds the value returned by
the getValue method to the nearest tick
void setToolTipText Sets the tooltip text that’s displayed if the user (String text) rests the mouse pointer over the
slider for a few moments.

To create a bare-bones slider, just call the JSlider constructor. You can create a slider that ranges from 0 to 100 like this:

slider = new JSlider();

Here the slider variable is declared as a class variable of type JSlider.

If you want to specify the minimum and maximum values, use this constructor:

slider = new JSlider(0, 50);

The slider lets the user choose a value from 0 to 50. The initial position of the knob is 25, midway between the minimum and maximum values.

To set a different initial value, use this constructor:

slider = new JSlider(0, 0, 50);

Here the slider ranges from 0 to 50, and the initial value is 0.

You usually want to add at least some adornments to the slider to make it more usable. The slider you see has minimum and maximum tick-mark values with labels visible. Here’s the code used to create it:

slider = new JSlider(0, 50, 0);

Note: Even if you set the major and minor tick-spacing values, the tick marks won’t appear onscreen unless you call setPaintTicks with the parameter set to true. The setPaintLabels method shows the labels along with the tick marks, and the setSnapToTicks method rounds the value to the nearest tick mark.

To get the value of the slider, you use the getValue method. Here’s a method called from the action listener attached to the OK button:

public void buttonOKClick()
 int level = slider.getValue();
  "Remember, this is for posterity.n"
   + "Tell do you feel?",
  "Level " + level,

Here a message box is displayed when the user clicks the OK button. The current setting of the slider component is retrieved and stored in an int variable named level, which is then used to create the title for the message box.

You can also add an event listener that reacts whenever the user changes the value of the slider. To do that, you use the addChangeListener method. The parameter must be an object that implements the ChangeListener interface, which defines a single method named stateChanged.

Because the ChangeListener interface has just one method, it qualifies as a functional interface and can be used in a Lambda expression. In most cases, the easiest way to implement a change listener is to create a method that contains the code you want executed when the slider control changes, then pass it via a Lambda expression to the slider control’s addChangeListener method.

Here’s an example of a method you might call when the state of a slider is changed:

public void sliderChanged()
 if (slider.getValue() == 50)
   "No! Not 50!",
   "The Machine",

To call this method in response to a change in the slider’s state, call the slider’s addChangeListener method with a a Lambda expression:

slider.addChangeListener(e -> sliderChanged() );

Then the stateChanged method is called whenever the user moves the knob to another position. It checks the value of the slider and displays a message box if the user has advanced the slider all the way to 50.