javax.swing

Class JSlider

public class JSlider extends JComponent implements SwingConstants, Accessible, ImageObserver, MenuContainer, 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 {@link DefaultBoundedRangeModel}.

A JSlider component has the following properties:

Property Stored in Bound?
extent model no
inverted slider yes
labelTable slider yes
majorTickSpacing slider yes
maximum model yes
minimum model yes
minorTickSpacing slider yes
model slider yes
orientation slider yes
paintLabels slider yes
paintTicks slider yes
snapToTicks slider yes
value model no
valueIsAdjusting model no

The various behavioural aspects of these properties follows:

Nested Class Summary
protected classJSlider.AccessibleJSlider
Provides the accessibility features for the JSlider component.
Field Summary
protected ChangeEventchangeEvent
The change event that is passed to all listeners of this slider.
protected ChangeListenerchangeListener
The listener that monitors the slider's model and forwards events to the slider's listeners (see createChangeListener()).
protected intmajorTickSpacing
The space/distance between major ticks.
protected intminorTickSpacing
The space/distance between minor ticks.
protected intorientation
The orientation (horizontal or vertical) of the slider.
protected BoundedRangeModelsliderModel
The model used to store the slider's range and current value.
protected booleansnapToTicks
Whether the slider snaps its values to ticks.
Constructor Summary
JSlider()
Creates a new horizontal JSlider instance with a minimum of 0, a maximum of 100, and a value of 50.
JSlider(int orientation)
Creates a new JSlider instance with the given orientation and a minimum of 0, a maximum of 100, and a value of 50.
JSlider(int minimum, int maximum)
Creates a new horizontal JSlider instance with the given maximum and minimum and a value that is halfway between the minimum and the maximum.
JSlider(int minimum, int maximum, int value)
Creates a new horizontal JSlider instance with the given minimum, maximum, and value.
JSlider(int orientation, int minimum, int maximum, int value)
Creates a new JSlider instance with the given orientation, minimum, maximum, and value.
JSlider(BoundedRangeModel model)
Creates a new horizontal JSlider instance with the given model.
Method Summary
voidaddChangeListener(ChangeListener listener)
Registers a listener with the slider so that it will receive {@link ChangeEvent} notifications.
protected ChangeListenercreateChangeListener()
Creates a {@link ChangeListener} that is added to the slider's model and forwards change events generated by the model to the listeners that are registered with the JSlider (by calling the {@link #fireStateChanged} method).
HashtablecreateStandardLabels(int increment)
Creates a hashtable of (Integer, JLabel) pairs that can be used as a label table for this slider.
HashtablecreateStandardLabels(int increment, int start)
Creates a hashtable of (Integer, JLabel) pairs that can be used as a label table for this slider.
protected voidfireStateChanged()
Sends a {@link ChangeEvent} to all registered listeners, with this slider as the source.
AccessibleContextgetAccessibleContext()
Returns the object that provides accessibility features for this JSlider component.
ChangeListener[]getChangeListeners()
Returns an array containing all the {@link ChangeListener} instances registered with this slider.
intgetExtent()
Returns the slider's extent value, obtained from the slider's model.
booleangetInverted()
Returns the flag that controls whether or not the value scale for the slider is inverted (the default value is false).
DictionarygetLabelTable()
Returns the label table for the slider.
intgetMajorTickSpacing()
Returns the distance between major tick marks along the slider's value scale.
intgetMaximum()
Returns the slider's maximum value (obtained from the slider's model).
intgetMinimum()
Returns the minimum value of the slider (from the slider's model).
intgetMinorTickSpacing()
Returns the distance between minor tick marks along the slider's value scale.
BoundedRangeModelgetModel()
Returns the slider's model, which stores the minimum, maximum and current values.
intgetOrientation()
Returns the orientation of the slider, either {@link JSlider#HORIZONTAL} or {@link JSlider#VERTICAL}.
booleangetPaintLabels()
Returns the flag that controls whether or not labels are painted for the tick marks along the slider.
booleangetPaintTicks()
Returns the flag that controls whether or not tick marks are painted along the slider's value scale.
booleangetPaintTrack()
Returns the flag that controls whether or not the track is painted.
booleangetSnapToTicks()
Returns the flag that controls whether the slider thumb will snap to ticks.
SliderUIgetUI()
Returns the slider's UI delegate.
StringgetUIClassID()
Returns the suffix ("SliderUI" in this case) used to determine the class name for a UI delegate that can provide the look and feel for a JSlider.
intgetValue()
Returns the slider's value (from the slider's model).
booleangetValueIsAdjusting()
Returns the valueIsAdjusting flag from the slider's model.
protected StringparamString()
Returns an implementation-dependent string describing the attributes of this JSlider.
voidremoveChangeListener(ChangeListener listener)
Removes a listener from this slider so that it will no longer receive {@link ChangeEvent} notifications from the slider.
voidsetExtent(int extent)
Sets the slider's extent value and sends a {@link ChangeEvent} to all registered listeners.
voidsetInverted(boolean inverted)
Sets the flag that controls whether or not the value scale for the slider is inverted and, if the new flag value is different to the old flag value, sends a {@link PropertyChangeEvent} to all registered listeners.
voidsetLabelTable(Dictionary table)
Sets the table of labels for the slider and sends a {@link PropertyChangeEvent} (with the property name "labelTable") to all registered listeners.
voidsetMajorTickSpacing(int spacing)
Sets the distance between major tick marks along the slider's value scale, and sends a {@link PropertyChangeEvent} (with the property name "majorTickSpacing") to all registered listeners.
voidsetMaximum(int maximum)
Sets the maximum value of the slider and fires a {@link PropertyChangeEvent} (with the property name "maximum") to all registered listeners.
voidsetMinimum(int minimum)
Sets the minimum value of the slider and fires a {@link PropertyChangeEvent} (with the property name "minimum") to all registered listeners.
voidsetMinorTickSpacing(int spacing)
Sets the distance between minor tick marks along the slider's value scale, and sends a {@link PropertyChangeEvent} (with the property name "minorTickSpacing") to all registered listeners.
voidsetModel(BoundedRangeModel model)
Sets the slider's model and sends a {@link PropertyChangeEvent} (with the property name "model") to all registered listeners.
voidsetOrientation(int orientation)
Sets the orientation for the slider and sends a {@link PropertyChangeEvent} (with the property name "orientation") to all registered listeners.
voidsetPaintLabels(boolean paint)
Sets the flag that controls whether or not labels are painted for the tick marks along the slider and sends a {@link PropertyChangeEvent} (with the property name "paintLabels") to all registered listeners.
voidsetPaintTicks(boolean paint)
Sets the flag that controls whether or not tick marks are painted along the slider's value scale, and sends a {@link PropertyChangeEvent} (with the property name "paintTicks") to all registered listeners.
voidsetPaintTrack(boolean paint)
Sets the flag that controls whether or not the track is painted, and sends a {@link PropertyChangeEvent} (for the "paintTrack" property) to all registered listeners.
voidsetSnapToTicks(boolean snap)
Sets the flag that controls whether the slider thumb will snap to ticks and sends a {@link PropertyChangeEvent} (with the property name 'snapToTicks') to all registered listeners.
voidsetUI(SliderUI ui)
Sets the slider's UI delegate.
voidsetValue(int value)
Sets the slider's value and sends a {@link ChangeEvent} to all registered listeners.
voidsetValueIsAdjusting(boolean adjusting)
Sets the valueIsAdjusting flag in the slider's model, and sends a {@link ChangeEvent} to all registered listeners.
protected voidupdateLabelUIs()
Resets the UI delegates for the labels in the labelTable to the default for the current look and feel.
voidupdateUI()
Sets this slider's UI delegate to the default (obtained from the {@link UIManager}) for the current look and feel.

Field Detail

changeEvent

protected transient ChangeEvent changeEvent
The change event that is passed to all listeners of this slider.

changeListener

protected ChangeListener changeListener
The listener that monitors the slider's model and forwards events to the slider's listeners (see createChangeListener()).

majorTickSpacing

protected int majorTickSpacing
The space/distance between major ticks.

minorTickSpacing

protected int minorTickSpacing
The space/distance between minor ticks.

orientation

protected int orientation
The orientation (horizontal or vertical) of the slider.

sliderModel

protected BoundedRangeModel sliderModel
The model used to store the slider's range and current value.

snapToTicks

protected boolean snapToTicks
Whether the slider snaps its values to ticks.

Constructor Detail

JSlider

public JSlider()
Creates a new horizontal JSlider instance with a minimum of 0, a maximum of 100, and a value of 50.

JSlider

public JSlider(int orientation)
Creates a new JSlider instance with the given orientation and a minimum of 0, a maximum of 100, and a value of 50.

Parameters: orientation The orientation of the slider ({@link #HORIZONTAL} or {@link #VERTICAL}).

Throws: IllegalArgumentException if orientation is not one of the specified values.

JSlider

public JSlider(int minimum, int maximum)
Creates a new horizontal JSlider instance with the given maximum and minimum and a value that is halfway between the minimum and the maximum.

Parameters: minimum The minimum value. maximum The maximum value.

Throws: IllegalArgumentException if minimum is greater than maximum.

JSlider

public JSlider(int minimum, int maximum, int value)
Creates a new horizontal JSlider instance with the given minimum, maximum, and value.

Parameters: minimum The minimum value. maximum The maximum value. value The initial value.

Throws: IllegalArgumentException if value is not in the specified range. IllegalArgumentException if minimum is greater than maximum.

JSlider

public JSlider(int orientation, int minimum, int maximum, int value)
Creates a new JSlider instance with the given orientation, minimum, maximum, and value.

Parameters: orientation The orientation of the slider ({@link #HORIZONTAL} or {@link #VERTICAL}). minimum The minimum value of the JSlider. maximum The maximum value of the JSlider. value The initial value of the JSlider.

Throws: IllegalArgumentException if orientation is not one of the specified values. IllegalArgumentException if value is not in the specified range. IllegalArgumentException if minimum is greater than maximum.

JSlider

public JSlider(BoundedRangeModel model)
Creates a new horizontal JSlider instance with the given model.

Parameters: model The model (null not permitted).

Throws: NullPointerException if model is null.

Method Detail

addChangeListener

public void addChangeListener(ChangeListener listener)
Registers a listener with the slider so that it will receive {@link ChangeEvent} notifications. Note that change events generated by the slider's model will be forwarded automatically to the slider's listeners.

Parameters: listener the listener to register.

See Also: removeChangeListener

createChangeListener

protected ChangeListener createChangeListener()
Creates a {@link ChangeListener} that is added to the slider's model and forwards change events generated by the model to the listeners that are registered with the JSlider (by calling the {@link #fireStateChanged} method).

Returns: A new listener.

createStandardLabels

public Hashtable createStandardLabels(int increment)
Creates a hashtable of (Integer, JLabel) pairs that can be used as a label table for this slider. The labels will start from the slider's minimum and increase by the increment. Each label will have a text string indicating its integer value.

Parameters: increment The increment between labels (must be > 0).

Returns: A hashtable containing the labels.

Throws: IllegalArgumentException if increment is not greater than zero.

createStandardLabels

public Hashtable createStandardLabels(int increment, int start)
Creates a hashtable of (Integer, JLabel) pairs that can be used as a label table for this slider. The labels will start from the given start value and increase by the increment. Each label will have a text string indicating its integer value.

Parameters: increment The increment between labels (must be > 0). start The value to start from.

Returns: A hashtable with the labels and their keys.

Throws: IllegalArgumentException if increment is not greater than zero, or start is not within the range of the model.

fireStateChanged

protected void fireStateChanged()
Sends a {@link ChangeEvent} to all registered listeners, with this slider as the source.

getAccessibleContext

public AccessibleContext getAccessibleContext()
Returns the object that provides accessibility features for this JSlider component.

Returns: The accessible context (an instance of {@link AccessibleJSlider}).

getChangeListeners

public ChangeListener[] getChangeListeners()
Returns an array containing all the {@link ChangeListener} instances registered with this slider. If no listeners are registered, this method returns an empty array.

Returns: An array array containing all the {@link ChangeListener} instances registered with this slider (possibly empty, but never null).

getExtent

public int getExtent()
Returns the slider's extent value, obtained from the slider's model.

Returns: The extent value.

See Also: JSlider

getInverted

public boolean getInverted()
Returns the flag that controls whether or not the value scale for the slider is inverted (the default value is false).

Returns: The flag that controls whether or not the value scale for the slider is inverted.

See Also: JSlider

getLabelTable

public Dictionary getLabelTable()
Returns the label table for the slider.

Returns: The label table for the slider (possibly null).

See Also: setLabelTable

getMajorTickSpacing

public int getMajorTickSpacing()
Returns the distance between major tick marks along the slider's value scale.

Returns: The amount of units between each major tick mark.

See Also: JSlider

getMaximum

public int getMaximum()
Returns the slider's maximum value (obtained from the slider's model).

Returns: The maximum value of the slider.

See Also: JSlider

getMinimum

public int getMinimum()
Returns the minimum value of the slider (from the slider's model).

Returns: The minimum value of the slider.

See Also: JSlider

getMinorTickSpacing

public int getMinorTickSpacing()
Returns the distance between minor tick marks along the slider's value scale.

Returns: The distance between minor tick marks along the slider's value scale.

See Also: JSlider

getModel

public BoundedRangeModel getModel()
Returns the slider's model, which stores the minimum, maximum and current values.

Returns: The slider's model.

See Also: setModel

getOrientation

public int getOrientation()
Returns the orientation of the slider, either {@link JSlider#HORIZONTAL} or {@link JSlider#VERTICAL}.

Returns: The orientation of the slider.

See Also: JSlider

getPaintLabels

public boolean getPaintLabels()
Returns the flag that controls whether or not labels are painted for the tick marks along the slider.

Returns: Whether labels will be painted.

See Also: JSlider

getPaintTicks

public boolean getPaintTicks()
Returns the flag that controls whether or not tick marks are painted along the slider's value scale.

Returns: true if tick marks should be painted, and false if tick marks should not be painted.

See Also: JSlider

getPaintTrack

public boolean getPaintTrack()
Returns the flag that controls whether or not the track is painted.

Returns: Whether the track will be painted.

See Also: JSlider

getSnapToTicks

public boolean getSnapToTicks()
Returns the flag that controls whether the slider thumb will snap to ticks. Sliders that snap to ticks will automatically move the thumb to the nearest tick mark.

Returns: true if the slider thumb automatically.

See Also: JSlider

getUI

public SliderUI getUI()
Returns the slider's UI delegate.

Returns: The slider's UI delegate.

getUIClassID

public String getUIClassID()
Returns the suffix ("SliderUI" in this case) used to determine the class name for a UI delegate that can provide the look and feel for a JSlider.

Returns: "SliderUI".

getValue

public int getValue()
Returns the slider's value (from the slider's model).

Returns: The value of the slider.

See Also: JSlider

getValueIsAdjusting

public boolean getValueIsAdjusting()
Returns the valueIsAdjusting flag from the slider's model.

Returns: The valueIsAdjusting flag from the slider's model.

See Also: JSlider

paramString

protected String paramString()
Returns an implementation-dependent string describing the attributes of this JSlider.

Returns: A string describing the attributes of this JSlider (never null).

removeChangeListener

public void removeChangeListener(ChangeListener listener)
Removes a listener from this slider so that it will no longer receive {@link ChangeEvent} notifications from the slider.

Parameters: listener The listener to remove.

See Also: addChangeListener

setExtent

public void setExtent(int extent)
Sets the slider's extent value and sends a {@link ChangeEvent} to all registered listeners. Note that the model will fire a change event to all of its registered listeners first (with the model as the event source) and then the slider will fire another change event to all of its registered listeners (this time with the slider as the event source).

Parameters: extent The extent value for this slider.

See Also: getExtent

setInverted

public void setInverted(boolean inverted)
Sets the flag that controls whether or not the value scale for the slider is inverted and, if the new flag value is different to the old flag value, sends a {@link PropertyChangeEvent} to all registered listeners. Typically, a horizontal slider will display a scale that increases from left to right, but this is reversed if the 'inverted' flag is set to true. Similarly, a vertical slider will display a scale that increases from bottom to top, and this is reversed if the 'inverted' flag is set to true.

Parameters: inverted the new flag value.

See Also: getInverted

setLabelTable

public void setLabelTable(Dictionary table)
Sets the table of labels for the slider and sends a {@link PropertyChangeEvent} (with the property name "labelTable") to all registered listeners.

Parameters: table the table of labels (null permitted).

See Also: getLabelTable

setMajorTickSpacing

public void setMajorTickSpacing(int spacing)
Sets the distance between major tick marks along the slider's value scale, and sends a {@link PropertyChangeEvent} (with the property name "majorTickSpacing") to all registered listeners.

Parameters: spacing the distance between major tick marks.

See Also: getMajorTickSpacing

setMaximum

public void setMaximum(int maximum)
Sets the maximum value of the slider and fires a {@link PropertyChangeEvent} (with the property name "maximum") to all registered listeners. Note that:

Parameters: maximum The maximum value of the slider.

See Also: getMaximum

setMinimum

public void setMinimum(int minimum)
Sets the minimum value of the slider and fires a {@link PropertyChangeEvent} (with the property name "minimum") to all registered listeners. Note that:

Parameters: minimum The minimum value of the slider.

See Also: getMinimum

setMinorTickSpacing

public void setMinorTickSpacing(int spacing)
Sets the distance between minor tick marks along the slider's value scale, and sends a {@link PropertyChangeEvent} (with the property name "minorTickSpacing") to all registered listeners.

Parameters: spacing the distance between minor tick marks.

See Also: getMinorTickSpacing

setModel

public void setModel(BoundedRangeModel model)
Sets the slider's model and sends a {@link PropertyChangeEvent} (with the property name "model") to all registered listeners. The change listener that the slider registered with the original model is removed and added to the new model (this ensures that {@link ChangeEvent} notifications generated by the model are automatically forwarded to listeners that are registered with the slider).

Parameters: model The model to use with the slider.

See Also: getModel

setOrientation

public void setOrientation(int orientation)
Sets the orientation for the slider and sends a {@link PropertyChangeEvent} (with the property name "orientation") to all registered listeners.

Parameters: orientation the orientation (one of {@link JSlider#HORIZONTAL} or {@link JSlider#VERTICAL}).

Throws: IllegalArgumentException if orientation is not one of the permitted values.

See Also: getOrientation

setPaintLabels

public void setPaintLabels(boolean paint)
Sets the flag that controls whether or not labels are painted for the tick marks along the slider and sends a {@link PropertyChangeEvent} (with the property name "paintLabels") to all registered listeners.

Parameters: paint Whether labels will be painted.

See Also: getPaintLabels

setPaintTicks

public void setPaintTicks(boolean paint)
Sets the flag that controls whether or not tick marks are painted along the slider's value scale, and sends a {@link PropertyChangeEvent} (with the property name "paintTicks") to all registered listeners. In addition to setting this property to true, one or both of the minor tick spacing and major tick spacing attributes must be set to a value greater than 0 in order for ticks to be painted.

Parameters: paint Whether ticks will be painted.

See Also: getPaintTicks

setPaintTrack

public void setPaintTrack(boolean paint)
Sets the flag that controls whether or not the track is painted, and sends a {@link PropertyChangeEvent} (for the "paintTrack" property) to all registered listeners.

Parameters: paint Whether the track will be painted.

See Also: getPaintTrack

setSnapToTicks

public void setSnapToTicks(boolean snap)
Sets the flag that controls whether the slider thumb will snap to ticks and sends a {@link PropertyChangeEvent} (with the property name 'snapToTicks') to all registered listeners. Sliders that snap to ticks will automatically move the thumb to the nearest tick mark.

Parameters: snap the new flag value.

See Also: getSnapToTicks

setUI

public void setUI(SliderUI ui)
Sets the slider's UI delegate.

Parameters: ui the UI delegate.

setValue

public void setValue(int value)
Sets the slider's value and sends a {@link ChangeEvent} to all registered listeners. Note that the model will fire a change event to all of its registered listeners first (with the model as the event source) and then the slider will fire another change event to all of its registered listeners (this time with the slider as the event source).

Parameters: value the new value.

See Also: getValue

setValueIsAdjusting

public void setValueIsAdjusting(boolean adjusting)
Sets the valueIsAdjusting flag in the slider's model, and sends a {@link ChangeEvent} to all registered listeners.

Parameters: adjusting the new flag value.

See Also: getValueIsAdjusting

updateLabelUIs

protected void updateLabelUIs()
Resets the UI delegates for the labels in the labelTable to the default for the current look and feel.

updateUI

public void updateUI()
Sets this slider's UI delegate to the default (obtained from the {@link UIManager}) for the current look and feel.