javax.swing
public class JSlider extends JComponent implements SwingConstants, Accessible, ImageObserver, MenuContainer, Serializable
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:
ChangeEvent
to its change listeners.
PropertyChangeEvent
to its property change listeners.
ChangeEvent
to
its listeners, which include the slider.
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.
Modifier and Type | Class and Description |
---|---|
protected class |
JSlider.AccessibleJSlider
Provides the accessibility features for the
JSlider
component. |
JComponent.AccessibleJComponent
Container.AccessibleAWTContainer
Component.AccessibleAWTComponent, Component.BltBufferStrategy, Component.FlipBufferStrategy
Modifier and Type | Field and Description |
---|---|
protected ChangeEvent |
changeEvent
The change event that is passed to all listeners of this slider.
|
protected ChangeListener |
changeListener
The listener that monitors the slider's model and forwards events to the
slider's listeners (see
createChangeListener() ). |
protected int |
majorTickSpacing
The space/distance between major ticks.
|
protected int |
minorTickSpacing
The space/distance between minor ticks.
|
protected int |
orientation
The orientation (horizontal or vertical) of the slider.
|
protected BoundedRangeModel |
sliderModel
The model used to store the slider's range and current value.
|
protected boolean |
snapToTicks
Whether the slider snaps its values to ticks.
|
accessibleContext, listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
BOTTOM, CENTER, EAST, HORIZONTAL, LEADING, LEFT, NEXT, NORTH, NORTH_EAST, NORTH_WEST, PREVIOUS, RIGHT, SOUTH, SOUTH_EAST, SOUTH_WEST, TOP, TRAILING, VERTICAL, WEST
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
Constructor and Description |
---|
JSlider()
Creates a new horizontal
JSlider instance with a minimum of
0, a maximum of 100, and a value of 50. |
JSlider(BoundedRangeModel model)
Creates a new horizontal
JSlider instance with the given
model. |
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. |
Modifier and Type | Method and Description |
---|---|
void |
addChangeListener(ChangeListener listener)
Registers a listener with the slider so that it will receive
ChangeEvent notifications. |
protected ChangeListener |
createChangeListener()
Creates a
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
fireStateChanged() method). |
Hashtable |
createStandardLabels(int increment)
Creates a hashtable of
(Integer, JLabel) pairs that can be
used as a label table for this slider. |
Hashtable |
createStandardLabels(int increment,
int start)
Creates a hashtable of
(Integer, JLabel) pairs that can be
used as a label table for this slider. |
protected void |
fireStateChanged()
Sends a
ChangeEvent to all registered listeners, with this slider
as the source. |
AccessibleContext |
getAccessibleContext()
Returns the object that provides accessibility features for this
JSlider component. |
ChangeListener[] |
getChangeListeners()
Returns an array containing all the
ChangeListener instances
registered with this slider. |
int |
getExtent()
Returns the slider's extent value, obtained from the slider's model.
|
boolean |
getInverted()
Returns the flag that controls whether or not the value scale for the
slider is inverted (the default value is
false ). |
Dictionary |
getLabelTable()
Returns the label table for the slider.
|
int |
getMajorTickSpacing()
Returns the distance between major tick marks along the slider's value
scale.
|
int |
getMaximum()
Returns the slider's maximum value (obtained from the slider's model).
|
int |
getMinimum()
Returns the minimum value of the slider (from the slider's model).
|
int |
getMinorTickSpacing()
Returns the distance between minor tick marks along the slider's value
scale.
|
BoundedRangeModel |
getModel()
Returns the slider's model, which stores the minimum, maximum and current
values.
|
int |
getOrientation()
Returns the orientation of the slider, either
SwingConstants.HORIZONTAL
or SwingConstants.VERTICAL . |
boolean |
getPaintLabels()
Returns the flag that controls whether or not labels are painted for the
tick marks along the slider.
|
boolean |
getPaintTicks()
Returns the flag that controls whether or not tick marks are painted along
the slider's value scale.
|
boolean |
getPaintTrack()
Returns the flag that controls whether or not the track is painted.
|
boolean |
getSnapToTicks()
Returns the flag that controls whether the slider thumb will snap to ticks.
|
SliderUI |
getUI()
Returns the slider's UI delegate.
|
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 . |
int |
getValue()
Returns the slider's value (from the slider's model).
|
boolean |
getValueIsAdjusting()
Returns the
valueIsAdjusting flag from the slider's model. |
protected String |
paramString()
Returns an implementation-dependent string describing the attributes of
this
JSlider . |
void |
removeChangeListener(ChangeListener listener)
Removes a listener from this slider so that it will no longer receive
ChangeEvent notifications from the slider. |
void |
setExtent(int extent)
Sets the slider's extent value and sends a
ChangeEvent to all
registered listeners. |
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
PropertyChangeEvent to all registered listeners. |
void |
setLabelTable(Dictionary table)
Sets the table of labels for the slider and sends a
PropertyChangeEvent (with the property name "labelTable") to all
registered listeners. |
void |
setMajorTickSpacing(int spacing)
Sets the distance between major tick marks along the slider's value scale,
and sends a
PropertyChangeEvent (with the property name
"majorTickSpacing") to all registered listeners. |
void |
setMaximum(int maximum)
Sets the maximum value of the slider and fires a
PropertyChangeEvent (with the property name "maximum") to all
registered listeners. |
void |
setMinimum(int minimum)
Sets the minimum value of the slider and fires a
PropertyChangeEvent (with the property name "minimum") to all
registered listeners. |
void |
setMinorTickSpacing(int spacing)
Sets the distance between minor tick marks along the slider's value scale,
and sends a
PropertyChangeEvent (with the property name
"minorTickSpacing") to all registered listeners. |
void |
setModel(BoundedRangeModel model)
Sets the slider's model and sends a
PropertyChangeEvent (with the
property name "model") to all registered listeners. |
void |
setOrientation(int orientation)
Sets the orientation for the slider and sends a
PropertyChangeEvent (with the property name "orientation") to all
registered listeners. |
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
PropertyChangeEvent (with
the property name "paintLabels") to all registered listeners. |
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
PropertyChangeEvent (with
the property name "paintTicks") to all registered listeners. |
void |
setPaintTrack(boolean paint)
Sets the flag that controls whether or not the track is painted, and
sends a
PropertyChangeEvent (for the "paintTrack" property) to all
registered listeners. |
void |
setSnapToTicks(boolean snap)
Sets the flag that controls whether the slider thumb will snap to ticks
and sends a
PropertyChangeEvent (with the property name
'snapToTicks') to all registered listeners. |
void |
setUI(SliderUI ui)
Sets the slider's UI delegate.
|
void |
setValue(int value)
Sets the slider's value and sends a
ChangeEvent to all
registered listeners. |
void |
setValueIsAdjusting(boolean adjusting)
Sets the
valueIsAdjusting flag in the slider's model, and
sends a ChangeEvent to all registered listeners. |
protected void |
updateLabelUIs()
Resets the UI delegates for the labels in the
labelTable to
the default for the current look and feel. |
void |
updateUI()
Sets this slider's UI delegate to the default (obtained from the
UIManager ) for the current look and feel. |
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setNextFocusableComponent, setOpaque, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
add, add, add, add, add, addContainerListener, addImpl, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalKeys, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusDownCycle, validate, validateTree
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setMaximumSize, setMinimumSize, setName, setPreferredSize, setSize, setSize, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
imageUpdate
getFont, postEvent, remove
protected BoundedRangeModel sliderModel
protected int majorTickSpacing
protected int minorTickSpacing
protected boolean snapToTicks
protected int orientation
protected ChangeListener changeListener
createChangeListener()
).protected transient ChangeEvent changeEvent
public JSlider()
JSlider
instance with a minimum of
0, a maximum of 100, and a value of 50.public JSlider(int orientation)
JSlider
instance with the given orientation
and a minimum of 0, a maximum of 100, and a value of 50.orientation
- The orientation of the slider (SwingConstants.HORIZONTAL
or
SwingConstants.VERTICAL
).IllegalArgumentException
- if orientation
is not one of
the specified values.public JSlider(int minimum, int maximum)
JSlider
instance with the given
maximum and minimum and a value that is halfway between the minimum and the
maximum.minimum
- The minimum value.maximum
- The maximum value.IllegalArgumentException
- if minimum
is greater than
maximum
.public JSlider(int minimum, int maximum, int value)
JSlider
instance with the given
minimum, maximum, and value.minimum
- The minimum value.maximum
- The maximum value.value
- The initial value.IllegalArgumentException
- if value
is not in the
specified range.IllegalArgumentException
- if minimum
is greater than
maximum
.public JSlider(int orientation, int minimum, int maximum, int value)
JSlider
instance with the given orientation,
minimum, maximum, and value.orientation
- The orientation of the slider (SwingConstants.HORIZONTAL
or
SwingConstants.VERTICAL
).minimum
- The minimum value of the JSlider.maximum
- The maximum value of the JSlider.value
- The initial value of the JSlider.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
.public JSlider(BoundedRangeModel model)
JSlider
instance with the given
model.model
- The model (null
not permitted).NullPointerException
- if model
is null
.public int getValue()
setValue(int)
public void setValue(int value)
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).value
- the new value.getValue()
public void setUI(SliderUI ui)
ui
- the UI delegate.public void updateUI()
UIManager
) for the current look and feel.updateUI
in class JComponent
public String getUIClassID()
"SliderUI"
in this case) used to
determine the class name for a UI delegate that can provide the look and
feel for a JSlider
.getUIClassID
in class JComponent
"SliderUI"
.JComponent.setUI(javax.swing.plaf.ComponentUI)
,
JComponent.updateUI()
protected ChangeListener createChangeListener()
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
fireStateChanged()
method).public void addChangeListener(ChangeListener listener)
ChangeEvent
notifications. Note that change events generated
by the slider's model will be forwarded automatically to the slider's
listeners.listener
- the listener to register.removeChangeListener(ChangeListener)
public void removeChangeListener(ChangeListener listener)
ChangeEvent
notifications from the slider.listener
- The listener to remove.addChangeListener(ChangeListener)
protected void fireStateChanged()
ChangeEvent
to all registered listeners, with this slider
as the source.public ChangeListener[] getChangeListeners()
ChangeListener
instances
registered with this slider. If no listeners are registered, this method
returns an empty array.ChangeListener
instances
registered with this slider (possibly empty, but never
null
).public BoundedRangeModel getModel()
setModel(BoundedRangeModel)
public void setModel(BoundedRangeModel model)
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 ChangeEvent
notifications
generated by the model are automatically forwarded to listeners that are
registered with the slider).model
- The model to use with the slider.getModel()
public int getMinimum()
setMinimum(int)
public void setMinimum(int minimum)
PropertyChangeEvent
(with the property name "minimum") to all
registered listeners. Note that:
getModel()
);ChangeEvent
.minimum
- The minimum value of the slider.getMinimum()
public int getMaximum()
setMaximum(int)
public void setMaximum(int maximum)
PropertyChangeEvent
(with the property name "maximum") to all
registered listeners. Note that:
getModel()
);ChangeEvent
.maximum
- The maximum value of the slider.getMaximum()
public boolean getValueIsAdjusting()
valueIsAdjusting
flag from the slider's model.valueIsAdjusting
flag from the slider's model.setValueIsAdjusting(boolean)
public void setValueIsAdjusting(boolean adjusting)
valueIsAdjusting
flag in the slider's model, and
sends a ChangeEvent
to all registered listeners.adjusting
- the new flag value.getValueIsAdjusting()
public int getExtent()
setExtent(int)
public void setExtent(int extent)
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).extent
- The extent value for this slider.getExtent()
public int getOrientation()
SwingConstants.HORIZONTAL
or SwingConstants.VERTICAL
.setOrientation(int)
public void setOrientation(int orientation)
PropertyChangeEvent
(with the property name "orientation") to all
registered listeners.orientation
- the orientation (one of SwingConstants.HORIZONTAL
or
SwingConstants.VERTICAL
).IllegalArgumentException
- if orientation
is not one of
the permitted values.getOrientation()
public Dictionary getLabelTable()
null
).setLabelTable(Dictionary)
public void setLabelTable(Dictionary table)
PropertyChangeEvent
(with the property name "labelTable") to all
registered listeners.table
- the table of labels (null
permitted).getLabelTable()
protected void updateLabelUIs()
labelTable
to
the default for the current look and feel.public Hashtable createStandardLabels(int increment)
(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.increment
- The increment between labels (must be > 0).IllegalArgumentException
- if increment
is not greater
than zero.public Hashtable createStandardLabels(int increment, int start)
(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.increment
- The increment between labels (must be > 0).start
- The value to start from.IllegalArgumentException
- if increment
is not greater
than zero, or start
is not within the range of the
model.public boolean getInverted()
false
).setInverted(boolean)
public void setInverted(boolean inverted)
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
.inverted
- the new flag value.getInverted()
public int getMajorTickSpacing()
setMajorTickSpacing(int)
public void setMajorTickSpacing(int spacing)
PropertyChangeEvent
(with the property name
"majorTickSpacing") to all registered listeners.spacing
- the distance between major tick marks.getMajorTickSpacing()
public int getMinorTickSpacing()
setMinorTickSpacing(int)
public void setMinorTickSpacing(int spacing)
PropertyChangeEvent
(with the property name
"minorTickSpacing") to all registered listeners.spacing
- the distance between minor tick marks.getMinorTickSpacing()
public boolean getSnapToTicks()
true
if the slider thumb automatically.setSnapToTicks(boolean)
public void setSnapToTicks(boolean snap)
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.snap
- the new flag value.getSnapToTicks()
public boolean getPaintTicks()
true
if tick marks should be painted, and
false
if tick marks should not be painted.setPaintTicks(boolean)
public void setPaintTicks(boolean paint)
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.paint
- Whether ticks will be painted.getPaintTicks()
public boolean getPaintTrack()
setPaintTrack(boolean)
public void setPaintTrack(boolean paint)
PropertyChangeEvent
(for the "paintTrack" property) to all
registered listeners.paint
- Whether the track will be painted.getPaintTrack()
public boolean getPaintLabels()
setPaintLabels(boolean)
public void setPaintLabels(boolean paint)
PropertyChangeEvent
(with
the property name "paintLabels") to all registered listeners.paint
- Whether labels will be painted.getPaintLabels()
protected String paramString()
JSlider
.paramString
in class JComponent
JSlider
(never null
).public AccessibleContext getAccessibleContext()
JSlider
component.getAccessibleContext
in interface Accessible
getAccessibleContext
in class JComponent
JSlider.AccessibleJSlider
).