| JavaTM 2 Platform Std. Ed. v1.6.0
javax.swing
Class JSlider
java.lang.Object
java.awt.Component
java.awt.Container
javax.swing.JComponent
javax.swing.JSlider
- All Implemented Interfaces:
- ImageObserver, MenuContainer, Serializable, Accessible, SwingConstants
public class JSlider - extends JComponent
- implements SwingConstants, Accessible
A component that lets the user graphically select a value by sliding
a knob within a bounded 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 JavaBeansTM
has been added to the java.beans package.
Please see XMLEncoder.
|
Nested Class Summary |
protected class |
JSlider.AccessibleJSlider
This class implements accessibility support for the
JSlider class. |
|
Field Summary |
protected 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. |
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. |
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. |
| Fields inherited from interface javax.swing.SwingConstants |
BOTTOM, CENTER, EAST, HORIZONTAL, LEADING, LEFT, NEXT, NORTH, NORTH_EAST, NORTH_WEST, PREVIOUS, RIGHT, SOUTH, SOUTH_EAST, SOUTH_WEST, TOP, TRAILING, VERTICAL, WEST |
|
Constructor Summary |
JSlider()
Creates a horizontal slider with the range 0 to 100 and
an initial value of 50. |
JSlider(BoundedRangeModel brm)
Creates a horizontal slider using the specified
BoundedRangeModel. |
JSlider(int orientation)
Creates a slider using the specified orientation with the
range 0 to 100 and an initial value of 50. |
JSlider(int min,
int max)
Creates a horizontal slider using the specified min and max
with an initial value equal to the average of the min plus max. |
JSlider(int min,
int max,
int value)
Creates a horizontal slider using the specified min, max and value. |
JSlider(int orientation,
int min,
int max,
int value)
Creates a slider with the specified orientation and the
specified minimum, maximum, and initial values. |
|
Method Summary |
void |
addChangeListener(ChangeListener l)
Adds a ChangeListener to the slider. |
protected ChangeListener |
createChangeListener()
Subclasses that want to handle ChangeEvents
from the model differently
can override this to return
an instance of a custom ChangeListener implementation. |
Hashtable |
createStandardLabels(int increment)
Creates a Hashtable of numerical text labels, starting at the
slider minimum, and using the increment specified. |
Hashtable |
createStandardLabels(int increment,
int 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. |
AccessibleContext |
getAccessibleContext()
Gets the AccessibleContext associated with this JSlider. |
ChangeListener[] |
getChangeListeners()
Returns an array of all the ChangeListeners added
to this JSlider with addChangeListener(). |
int |
getExtent()
Returns the "extent" from the BoundedRangeModel. |
boolean |
getInverted()
Returns true if the value-range shown for the slider is reversed, |
Dictionary |
getLabelTable()
Returns the dictionary of what labels to draw at which values. |
int |
getMajorTickSpacing()
This method returns the major tick spacing. |
int |
getMaximum()
Returns the maximum value supported by the slider
from the BoundedRangeModel. |
int |
getMinimum()
Returns the minimum value supported by the slider
from the BoundedRangeModel. |
int |
getMinorTickSpacing()
This method returns the minor tick spacing. |
BoundedRangeModel |
getModel()
Returns the BoundedRangeModel that handles the slider's three
fundamental properties: minimum, maximum, value. |
int |
getOrientation()
Return this slider's vertical or horizontal orientation. |
boolean |
getPaintLabels()
Tells if labels are to be painted. |
boolean |
getPaintTicks()
Tells if tick marks are to be painted. |
boolean |
getPaintTrack()
Tells if the track (area the slider slides in) is to be painted. |
boolean |
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. |
SliderUI |
getUI()
Gets the UI object which implements the L&F for this component. |
String |
getUIClassID()
Returns the name of the L&F class that renders this component. |
int |
getValue()
Returns the slider's current value
from the BoundedRangeModel. |
boolean |
getValueIsAdjusting()
Returns the valueIsAdjusting property from the model. |
protected String |
paramString()
Returns a string representation of this JSlider. |
void |
removeChangeListener(ChangeListener l)
Removes a ChangeListener from the slider. |
void |
setExtent(int extent)
Sets the size of the range "covered" by the knob. |
void |
setFont(Font font)
Sets the font for this component. |
void |
setInverted(boolean b)
Specify true to reverse the value-range shown for the slider and false to
put the value range in the normal order. |
void |
setLabelTable(Dictionary labels)
Used to specify what label will be drawn at any given value. |
void |
setMajorTickSpacing(int n)
This method sets the major tick spacing. |
void |
setMaximum(int maximum)
Sets the slider's maximum value to maximum. |
void |
setMinimum(int minimum)
Sets the slider's minimum value to minimum. |
void |
setMinorTickSpacing(int n)
This method sets the minor tick spacing. |
void |
setModel(BoundedRangeModel newModel)
Sets the BoundedRangeModel that handles the slider's three
fundamental properties: minimum, maximum, value. |
void |
setOrientation(int orientation)
Set the slider's orientation to either SwingConstants.VERTICAL or
SwingConstants.HORIZONTAL. |
void |
setPaintLabels(boolean b)
Determines whether labels are painted on the slider. |
void |
setPaintTicks(boolean b)
Determines whether tick marks are painted on the slider. |
void |
setPaintTrack(boolean b)
Determines whether the track is painted on the slider. |
void |
setSnapToTicks(boolean 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. |
void |
setUI(SliderUI ui)
Sets the UI object which implements the L&F for this component. |
void |
setValue(int n)
Sets the slider's current value to n. |
void |
setValueIsAdjusting(boolean b)
Sets the model's valueIsAdjusting property. |
protected void |
updateLabelUIs()
Updates the UIs for the labels in the label table by calling
updateUI on each label. |
void |
updateUI()
Resets the UI property to a value from the current look and feel. |
| Methods inherited from class javax.swing.JComponent |
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBaseline, getBaselineResizeBehavior, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseEvent, 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, setFocusTraversalKeys, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update |
| Methods inherited from class java.awt.Container |
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, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusBackward, transferFocusDownCycle, validate, validateTree |
| Methods inherited from class java.awt.Component |
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, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, 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, 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, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusUpCycle |
sliderModel
protected BoundedRangeModel sliderModel
- The data model that handles the numeric maximum value,
minimum value, and current-position value for the slider.
majorTickSpacing
protected int majorTickSpacing
- The number of values between the major tick marks -- the
larger marks that break up the minor tick marks.
minorTickSpacing
protected int minorTickSpacing
- The number of values between the minor tick marks -- the
smaller marks that occur between the major tick marks.
- See Also:
setMinorTickSpacing(int)
snapToTicks
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. The default is false.
- See Also:
setSnapToTicks(boolean)
orientation
protected int orientation
- Whether the slider is horizontal or vertical
The default is horizontal.
- See Also:
setOrientation(int)
changeListener
protected ChangeListener changeListener
- The changeListener (no suffix) is the listener we add to the
slider's model. This listener is initialized to the
ChangeListener returned from createChangeListener,
which by default just forwards events
to ChangeListeners (if any) added directly to the slider.
- See Also:
addChangeListener(javax.swing.event.ChangeListener),
createChangeListener()
changeEvent
protected transient ChangeEvent changeEvent
- Only one
ChangeEvent is needed per slider instance since the
event's only (read-only) state is the source property. The source
of events generated here is always "this". The event is lazily
created the first time that an event notification is fired.
- See Also:
fireStateChanged()
JSlider
public JSlider()
- Creates a horizontal slider with the range 0 to 100 and
an initial value of 50.
JSlider
public JSlider(int orientation)
- Creates a slider using the specified orientation with the
range
0 to 100 and an initial value of 50.
The orientation can be
either SwingConstants.VERTICAL or
SwingConstants.HORIZONTAL.
- Parameters:
orientation - the orientation of the slider
- Throws:
IllegalArgumentException - if orientation is not one of VERTICAL, HORIZONTAL- See Also:
setOrientation(int)
JSlider
public JSlider(int min,
int max)
- Creates a horizontal slider using the specified min and max
with an initial value equal to the average of the min plus max.
The BoundedRangeModel that holds the slider's data
handles any issues that may arise from improperly setting the
minimum and maximum values on the slider. See the
BoundedRangeModel documentation for details.
- Parameters:
min - the minimum value of the slidermax - the maximum value of the slider- See Also:
BoundedRangeModel,
setMinimum(int),
setMaximum(int)
JSlider
public JSlider(int min,
int max,
int value)
- Creates a horizontal slider using the specified min, max and value.
The BoundedRangeModel that holds the slider's data
handles any issues that may arise from improperly setting the
minimum, initial, and maximum values on the slider. See the
BoundedRangeModel documentation for details.
- Parameters:
min - the minimum value of the slidermax - the maximum value of the slidervalue - the initial value of the slider- See Also:
BoundedRangeModel,
setMinimum(int),
setMaximum(int),
setValue(int)
JSlider
public JSlider(int orientation,
int min,
int max,
int value)
- Creates a slider with the specified orientation and the
specified minimum, maximum, and initial values.
The orientation can be
either
SwingConstants.VERTICAL or
SwingConstants.HORIZONTAL.
The BoundedRangeModel that holds the slider's data
handles any issues that may arise from improperly setting the
minimum, initial, and maximum values on the slider. See the
BoundedRangeModel documentation for details.
- Parameters:
orientation - the orientation of the slidermin - the minimum value of the slidermax - the maximum value of the slidervalue - the initial value of the slider
- Throws:
IllegalArgumentException - if orientation is not one of VERTICAL, HORIZONTAL- See Also:
BoundedRangeModel,
setOrientation(int),
setMinimum(int),
setMaximum(int),
setValue(int)
JSlider
public JSlider(BoundedRangeModel brm)
- Creates a horizontal slider using the specified
BoundedRangeModel.
getUI
public SliderUI getUI()
- Gets the UI object which implements the L&F for this component.
- Returns:
- the SliderUI object that implements the Slider L&F
setUI
public void setUI(SliderUI ui)
- Sets the UI object which implements the L&F for this component.
- Parameters:
ui - the SliderUI L&F object- See Also:
UIDefaults.getUI(javax.swing.JComponent)
updateUI
public void updateUI()
- Resets the UI property to a value from the current look and feel.
- Overrides:
updateUI in class JComponent
- See Also:
JComponent.updateUI()
getUIClassID
public String getUIClassID()
- Returns the name of the L&F class that renders this component.
- Overrides:
getUIClassID in class JComponent
- Returns:
- "SliderUI"
- See Also:
JComponent.getUIClassID(),
UIDefaults.getUI(javax.swing.JComponent)
createChangeListener
protected ChangeListener createChangeListener()
- Subclasses that want to handle
ChangeEvents
from the model differently
can override this to return
an instance of a custom ChangeListener implementation.
The default ChangeListener simply calls the
fireStateChanged method to forward ChangeEvents
to the ChangeListeners that have been added directly to the
slider.
- See Also:
changeListener,
fireStateChanged(),
ChangeListener,
BoundedRangeModel
addChangeListener
public void addChangeListener(ChangeListener l)
- Adds a ChangeListener to the slider.
- Parameters:
l - the ChangeListener to add- See Also:
fireStateChanged(),
removeChangeListener(javax.swing.event.ChangeListener)
removeChangeListener
public void removeChangeListener(ChangeListener l)
- Removes a ChangeListener from the slider.
- Parameters:
l - the ChangeListener to remove- See Also:
fireStateChanged(),
addChangeListener(javax.swing.event.ChangeListener)
getChangeListeners
public ChangeListener[] getChangeListeners()
- Returns an array of all the
ChangeListeners added
to this JSlider with addChangeListener().
- Returns:
- all of the
ChangeListeners added or an empty
array if no listeners have been added - Since:
- 1.4
fireStateChanged
protected void fireStateChanged()
- Send a
ChangeEvent, whose source is this JSlider, to
all ChangeListeners that have registered interest in
ChangeEvents.
This method is called each time a ChangeEvent is received from
the model.
The event instance is created if necessary, and stored in
changeEvent.
- See Also:
addChangeListener(javax.swing.event.ChangeListener),
EventListenerList
getModel
public BoundedRangeModel getModel()
- Returns the
BoundedRangeModel that handles the slider's three
fundamental properties: minimum, maximum, value.
- Returns:
- the data model for this component
- See Also:
setModel(javax.swing.BoundedRangeModel),
BoundedRangeModel
setModel
public void setModel(BoundedRangeModel newModel)
- Sets the
BoundedRangeModel that handles the slider's three
fundamental properties: minimum, maximum, value.
Attempts to pass a null model to this method result in
undefined behavior, and, most likely, exceptions.
- Parameters:
newModel - the new, non-null BoundedRangeModel to use- See Also:
getModel(),
BoundedRangeModel
getValue
public int getValue()
- Returns the slider's current value
from the
BoundedRangeModel.
- Returns:
- the current value of the slider
- See Also:
setValue(int),
BoundedRangeModel.getValue()
setValue
public void setValue(int n)
- Sets the slider's current value to
n. This method
forwards the new value to the model.
The data model (an instance of BoundedRangeModel)
handles any mathematical
issues arising from assigning faulty values. See the
BoundedRangeModel documentation for details.
If the new value is different from the previous value,
all change listeners are notified.
- Parameters:
n - the new value- See Also:
getValue(),
addChangeListener(javax.swing.event.ChangeListener),
BoundedRangeModel.setValue(int)
getMinimum
public int getMinimum()
- Returns the minimum value supported by the slider
from the
BoundedRangeModel.
- Returns:
- the value of the model's minimum property
- See Also:
setMinimum(int),
BoundedRangeModel.getMinimum()
setMinimum
public void setMinimum(int minimum)
- Sets the slider's minimum value to
minimum. This method
forwards the new minimum value to the model.
The data model (an instance of BoundedRangeModel)
handles any mathematical
issues arising from assigning faulty values. See the
BoundedRangeModel documentation for details.
If the new minimum value is different from the previous minimum value,
all change listeners are notified.
- Parameters:
n - the new minimum- See Also:
getMinimum(),
addChangeListener(javax.swing.event.ChangeListener),
BoundedRangeModel.setMinimum(int)
getMaximum
public int getMaximum()
- Returns the maximum value supported by the slider
from the
BoundedRangeModel.
- Returns:
- the value of the model's maximum property
- See Also:
setMaximum(int),
BoundedRangeModel.getMaximum()
setMaximum
public void setMaximum(int maximum)
- Sets the slider's maximum value to
maximum. This method
forwards the new maximum value to the model.
The data model (an instance of BoundedRangeModel)
handles any mathematical
issues arising from assigning faulty values. See the
BoundedRangeModel documentation for details.
If the new maximum value is different from the previous maximum value,
all change listeners are notified.
- Parameters:
n - the new maximum- See Also:
getMaximum(),
addChangeListener(javax.swing.event.ChangeListener),
BoundedRangeModel.setMaximum(int)
getValueIsAdjusting
public boolean getValueIsAdjusting()
- Returns the
valueIsAdjusting property from the model. For
details on how this is used, see the setValueIsAdjusting
documentation.
- Returns:
- the value of the model's
valueIsAdjusting property - See Also:
setValueIsAdjusting(boolean)
setValueIsAdjusting
public void setValueIsAdjusting(boolean b)
- Sets the model's
valueIsAdjusting property. Slider look and
feel implementations should set this property to true when
a knob drag begins, and to false when the drag ends. The
slider model will not generate ChangeEvents while
valueIsAdjusting is true.
- Parameters:
b - the new value for the valueIsAdjusting property- See Also:
getValueIsAdjusting(),
BoundedRangeModel.setValueIsAdjusting(boolean)
getExtent
public int getExtent()
- Returns the "extent" from the
BoundedRangeModel.
This respresents the range of values "covered" by the knob.
- Returns:
- an int representing the extent
- See Also:
setExtent(int),
BoundedRangeModel.getExtent()
setExtent
public void setExtent(int extent)
- Sets the size of the range "covered" by the knob. Most look
and feel implementations will change the value by this amount
if the user clicks on either side of the knob. This method just
forwards the new extent value to the model.
The data model (an instance of BoundedRangeModel)
handles any mathematical
issues arising from assigning faulty values. See the
BoundedRangeModel d |