JSlider Class

Package: javax.swing

The JSlider class creates a slider control, which 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. Figure 5-10 shows a typical slider.

9781118239742-fg0510.tif

Figure 5-10

Constructors

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 maximum.

JSlider(int min, int max, int value)

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.

Methods

Method

Description

void addChangeListener(ChangeListener listener)

Adds a ChangeListener to listen for change events.

int getValue()

Gets the value indicated by the current position of the knob.

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 ...

Get Java For Dummies Quick Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.