APIdia Launch Partner
Top Description Inners Fields Constructors Methods
javax.swing

public Class JSlider

extends JComponent
implements SwingConstants, Accessible
Class Inheritance
All Implemented Interfaces
javax.accessibility.Accessible, javax.swing.SwingConstants
Annotations
@JavaBean
defaultProperty:UI
description:A component that supports selecting a integer value from a range.
@SwingContainer:false
@SuppressWarnings:serial
Imports
java.awt.Color, .Component, .Font, .Image, java.beans.BeanProperty, .JavaBean, .PropertyChangeEvent, .PropertyChangeListener, java.io.IOException, .ObjectOutputStream, .Serial, .Serializable, java.util.Dictionary, .Enumeration, .Hashtable, javax.accessibility.Accessible, .AccessibleAction, .AccessibleContext, .AccessibleRole, .AccessibleState, .AccessibleStateSet, .AccessibleValue, javax.swing.event.ChangeEvent, .ChangeListener, .EventListenerList, javax.swing.plaf.SliderUI, .UIResource

A component that lets the user graphically select a value by sliding a knob within a bounded interval. The knob is always positioned at the points that match integer values within the specified interval.

The slider can show both major tick marks, and minor tick marks between the major ones. The number of values between the tick marks is controlled with setMajorTickSpacing and setMinorTickSpacing. Painting of tick marks is controlled by setPaintTicks.

Sliders can also print text labels at regular intervals (or at arbitrary locations) along the slider track. Painting of labels is controlled by setLabelTable and setPaintLabels.

For further information and examples see How to Use Sliders, a section in The Java Tutorial.

Warning

Swing is not thread safe. For more information see Swing's Threading Policy.

Warning

Serialized objects of this class will not be compatible with future Swing releases. The current serialization support is appropriate for short term storage or RMI between applications running the same version of Swing. As of 1.4, support for long term storage of all JavaBeans has been added to the java.beans package. Please see java.beans.XMLEncoder.

Author
David Kloba
Since
1.2

Nested and Inner Type Summary

Modifier and TypeClass and Description
protected class
JSlider.AccessibleJSlider

This class implements accessibility support for the JSlider class.

private class
JSlider.ModelListener

We pass Change events along to the listeners with the slider (instead of the model itself) as the event source.

Field Summary

Modifier and TypeField and Description
protected transient ChangeEvent
changeEvent

Only one ChangeEvent is needed per slider instance since the event's only (read-only) state is the source property.

protected ChangeListener
changeListener

The changeListener (no suffix) is the listener we add to the slider's model.

private boolean
private Dictionary<K, V>
labelTable

Dictionary of what labels to draw at which values

protected int
majorTickSpacing

The number of values between the major tick marks -- the larger marks that break up the minor tick marks.

protected int
minorTickSpacing

The number of values between the minor tick marks -- the smaller marks that occur between the major tick marks.

protected int
orientation

Whether the slider is horizontal or vertical The default is horizontal.

private boolean
private boolean
private boolean
protected BoundedRangeModel
sliderModel

The data model that handles the numeric maximum value, minimum value, and current-position value for the slider.

protected boolean
snapToTicks

If true, the knob (and the data value it represents) resolve to the closest tick mark next to where the user positioned the knob.

pack-priv boolean
snapToValue

If true, the knob (and the data value it represents) resolve to the closest slider value next to where the user positioned the knob.

private static final String
Inherited from javax.swing.JComponent:
DEBUG_GRAPHICS_LOADEDfocusControllerivSourceComponentlistenerListpaintingChildTOOL_TIP_TEXT_KEYuiUNDEFINED_CONDITIONWHEN_ANCESTOR_OF_FOCUSED_COMPONENTWHEN_FOCUSEDWHEN_IN_FOCUSED_WINDOW

Constructor Summary

AccessConstructor and Description
public
JSlider()

Creates a horizontal slider with the range 0 to 100 and an initial value of 50.

public
JSlider(int
the orientation of the slider
orientation
)

Creates a slider using the specified orientation with the range 0 to 100 and an initial value of 50.

public
JSlider(int
the minimum value of the slider
min
,
int
the maximum value of the slider
max
)

Creates a horizontal slider using the specified min and max with an initial value equal to the average of the min plus max.

public
JSlider(int
the minimum value of the slider
min
,
int
the maximum value of the slider
max
,
int
the initial value of the slider
value
)

Creates a horizontal slider using the specified min, max and value.

public
JSlider(int
the orientation of the slider
orientation
,
int
the minimum value of the slider
min
,
int
the maximum value of the slider
max
,
int
the initial value of the slider
value
)

Creates a slider with the specified orientation and the specified minimum, maximum, and initial values.

public
JSlider(BoundedRangeModel
a BoundedRangeModel for the slider
brm
)

Creates a horizontal slider using the specified BoundedRangeModel.

Method Summary

Modifier and TypeMethod and Description
public void
addChangeListener(ChangeListener
the ChangeListener to add
l
)

Adds a ChangeListener to the slider.

private void
checkOrientation(int orientation)

protected ChangeListener

Returns:

a instance of new ChangeListener
createChangeListener
()

Subclasses that want to handle ChangeEvents from the model differently can override this to return an instance of a custom ChangeListener implementation.

public Hashtable<Integer, JComponent>

Returns:

a new Hashtable of labels
createStandardLabels
(int
distance between labels in the generated hashtable
increment
)

Creates a Hashtable of numerical text labels, starting at the slider minimum, and using the increment specified.

public Hashtable<Integer, JComponent>

Returns:

a new Hashtable of labels
createStandardLabels
(int
distance between labels in the generated hashtable
increment
,
int
value at which the labels will begin
start
)

Creates a Hashtable of numerical text labels, starting at the starting point specified, and using the increment specified.

protected void
fireStateChanged()

Send a ChangeEvent, whose source is this JSlider, to all ChangeListeners that have registered interest in ChangeEvents.

public AccessibleContext

Returns:

an AccessibleJSlider that serves as the AccessibleContext of this JSlider
getAccessibleContext
()

Overrides java.awt.Component.getAccessibleContext.

Implements javax.accessibility.Accessible.getAccessibleContext.

Gets the AccessibleContext associated with this JSlider.

public ChangeListener[]

Returns:

all of the ChangeListeners added or an empty array if no listeners have been added
getChangeListeners
()

Returns an array of all the ChangeListeners added to this JSlider with addChangeListener().

public int

Returns:

an int representing the extent
getExtent
()

Returns the "extent" from the BoundedRangeModel.

public boolean

Returns:

true if the slider values are reversed from their normal order
getInverted
()

Returns true if the value-range shown for the slider is reversed,

public Dictionary<K, V>

Returns:

the Dictionary containing labels and where to draw them
getLabelTable
()

Returns the dictionary of what labels to draw at which values.

public int

Returns:

the number of values between major ticks
getMajorTickSpacing
()

This method returns the major tick spacing.

public int

Returns:

the value of the model's maximum property
getMaximum
()

Returns the maximum value supported by the slider from the BoundedRangeModel.

public int

Returns:

the value of the model's minimum property
getMinimum
()

Returns the minimum value supported by the slider from the BoundedRangeModel.

public int

Returns:

the number of values between minor ticks
getMinorTickSpacing
()

This method returns the minor tick spacing.

public BoundedRangeModel

Returns:

the data model for this component
getModel
()

Returns the BoundedRangeModel that handles the slider's three fundamental properties: minimum, maximum, value.

public int

Returns:

SwingConstants.VERTICAL or SwingConstants.HORIZONTAL
getOrientation
()

Return this slider's vertical or horizontal orientation.

public boolean

Returns:

true if labels are painted, else false
getPaintLabels
()

Tells if labels are to be painted.

public boolean

Returns:

true if tick marks are painted, else false
getPaintTicks
()

Tells if tick marks are to be painted.

public boolean

Returns:

true if track is painted, else false
getPaintTrack
()

Tells if the track (area the slider slides in) is to be painted.

public boolean

Returns:

true if the value snaps to the nearest tick mark, else false
getSnapToTicks
()

Returns true if the knob (and the data value it represents) resolve to the closest tick mark next to where the user positioned the knob.

pack-priv boolean

Returns:

true if the value snaps to the nearest slider value, else false
getSnapToValue
()

Returns true if the knob (and the data value it represents) resolve to the closest slider value next to where the user positioned the knob.

public SliderUI

Returns:

the SliderUI object that implements the Slider L&F
getUI
()

Overrides javax.swing.JComponent.getUI.

Gets the UI object which implements the L&F for this component.

public String

Returns:

the string "SliderUI"
getUIClassID
()

Overrides javax.swing.JComponent.getUIClassID.

Returns the name of the L&F class that renders this component.

public int

Returns:

the current value of the slider
getValue
()

Returns the slider's current value from the BoundedRangeModel.

public boolean

Returns:

the value of the model's valueIsAdjusting property
getValueIsAdjusting
()

Returns the valueIsAdjusting property from the model.

public boolean
imageUpdate(Image img, int infoflags, int x, int y, int w, int h)

Overrides java.awt.Component.imageUpdate.

Implements java.awt.image.ImageObserver.imageUpdate.

Repaints the component when the image has changed.

protected String

Returns:

a string representation of this JSlider.
paramString
()

Overrides javax.swing.JComponent.paramString.

Returns a string representation of this JSlider.

public void
removeChangeListener(ChangeListener
the ChangeListener to remove
l
)

Removes a ChangeListener from the slider.

public void
setExtent(int
the new extent
extent
)

Sets the size of the range "covered" by the knob.

public void
setFont(Font font)

Overrides javax.swing.JComponent.setFont.

Sets the font for this component.

public void
setInverted(boolean
true to reverse the slider values from their normal order
b
)

Specify true to reverse the value-range shown for the slider and false to put the value range in the normal order.

public void
setLabelTable(Dictionary<K, V>
new Dictionary of labels, or null to remove all labels
labels
)

Used to specify what label will be drawn at any given value.

public void
setMajorTickSpacing(int
new value for the majorTickSpacing property
n
)

This method sets the major tick spacing.

public void
setMaximum(int
the new maximum
maximum
)

Sets the slider's maximum value to maximum.

public void
setMinimum(int
the new minimum
minimum
)

Sets the slider's minimum value to minimum.

public void
setMinorTickSpacing(int
new value for the minorTickSpacing property
n
)

This method sets the minor tick spacing.

public void
setModel(BoundedRangeModel
the new, non-null BoundedRangeModel to use
newModel
)

Sets the BoundedRangeModel that handles the slider's three fundamental properties: minimum, maximum, value.

public void
setOrientation(int
HORIZONTAL or VERTICAL
orientation
)

Set the slider's orientation to either SwingConstants.VERTICAL or SwingConstants.HORIZONTAL.

public void
setPaintLabels(boolean
whether or not to paint labels
b
)

Determines whether labels are painted on the slider.

public void
setPaintTicks(boolean
whether or not tick marks should be painted
b
)

Determines whether tick marks are painted on the slider.

public void
setPaintTrack(boolean
whether or not to paint the slider track
b
)

Determines whether the track is painted on the slider.

public void
setSnapToTicks(boolean
true to snap the knob to the nearest tick mark
b
)

Specifying true makes the knob (and the data value it represents) resolve to the closest tick mark next to where the user positioned the knob.

pack-priv void
setSnapToValue(boolean
true to snap the knob to the nearest slider value
b
)

Specifying true makes the knob (and the data value it represents) resolve to the closest slider value next to where the user positioned the knob.

public void
setUI(SliderUI
the SliderUI L&F object
ui
)

Sets the UI object which implements the L&F for this component.

public void
setValue(int
the new value
n
)

Sets the slider's current value to n.

public void
setValueIsAdjusting(boolean
the new value for the valueIsAdjusting property
b
)

Sets the model's valueIsAdjusting property.

private void
protected void
updateLabelUIs()

Updates the UIs for the labels in the label table by calling updateUI on each label.

public void
updateUI()

Overrides javax.swing.JComponent.updateUI.

Resets the UI property to a value from the current look and feel.

private void
writeObject(ObjectOutputStream s)

Hides javax.swing.JComponent.writeObject.

See readObject() and writeObject() in JComponent for more information about serialization in Swing.

Inherited from javax.swing.JComponent:
_paintImmediatelyaddAncestorListeneraddNotifyaddVetoableChangeListeneralwaysOnTopcheckIfChildObscuredBySiblingclientPropertyChangedcomponentInputMapChangedcomputeVisibleRectcomputeVisibleRectcompWriteObjectNotifycontainscreateToolTipdisabledndDonedropLocationForPointenablefirePropertyChangefirePropertyChangefirePropertyChangefireVetoableChangegetActionForKeyStrokegetActionMapgetActionMapgetAlignmentXgetAlignmentYgetAncestorListenersgetAutoscrollsgetBaselinegetBaselineResizeBehaviorgetBordergetBoundsgetClientPropertygetComponentGraphicsgetComponentPopupMenugetConditionForKeyStrokegetCreatedDoubleBuffergetDebugGraphicsOptionsgetDefaultLocalegetFontMetricsgetGraphicsgetGraphicsInvokedgetHeightgetInheritsPopupMenugetInputMapgetInputMapgetInputMapgetInputVerifiergetInsetsgetInsetsgetListenersgetLocationgetManagingFocusBackwardTraversalKeysgetManagingFocusForwardTraversalKeysgetMaximumSizegetMinimumSizegetNextFocusableComponentgetPopupLocationgetPreferredSizegetRegisteredKeyStrokesgetRootPanegetSizegetToolTipLocationgetToolTipTextgetToolTipTextgetTopLevelAncestorgetTransferHandlergetVerifyInputWhenFocusTargetgetVetoableChangeListenersgetVisibleRectgetWidthgetWriteObjCountergetXgetYgrabFocushideisDoubleBufferedisLightweightComponentisManagingFocusisOpaqueisOptimizedDrawingEnabledisPaintingisPaintingForPrintisPaintingOriginisPaintingTileisRequestFocusEnabledisValidateRootpaintpaintBorderpaintChildrenpaintComponentpaintForceDoubleBufferedpaintImmediatelypaintImmediatelypaintToOffscreenprintprintAllprintBorderprintChildrenprintComponentprocessComponentKeyEventprocessKeyBindingprocessKeyBindingsprocessKeyBindingsForAllComponentsprocessKeyEventprocessMouseEventprocessMouseMotionEventputClientPropertyrectangleIsObscuredregisterKeyboardActionregisterKeyboardActionremoveAncestorListenerremoveNotifyremoveVetoableChangeListenerrepaintrepaintrequestDefaultFocusrequestFocusrequestFocusrequestFocusInWindowrequestFocusInWindowresetKeyboardActionsreshaperevalidatesafelyGetGraphicssafelyGetGraphicsscrollRectToVisiblesetActionMapsetAlignmentXsetAlignmentYsetAutoscrollssetBackgroundsetBordersetComponentPopupMenusetCreatedDoubleBuffersetDebugGraphicsOptionssetDefaultLocalesetDoubleBufferedsetDropLocationsetEnabledsetFocusTraversalKeyssetForegroundsetInheritsPopupMenusetInputMapsetInputVerifiersetMaximumSizesetMinimumSizesetNextFocusableComponentsetOpaquesetPaintingChildsetPreferredSizesetRequestFocusEnabledsetToolTipTextsetTransferHandlersetUIsetUIPropertysetVerifyInputWhenFocusTargetsetVisiblesetWriteObjCountershouldDebugGraphicssuperProcessMouseMotionEventunregisterKeyboardActionupdate