JavaTM 2 Platform
Standard Edition

javax.swing
Class JSlider

java.lang.Object
  |
  +--java.awt.Component
        |
        +--java.awt.Container
              |
              +--javax.swing.JComponent
                    |
                    +--javax.swing.JSlider

public class JSlider
extends JComponent
implements SwingConstants, Accessible

A component that lets the user graphically select a value by slding a knob within a bounded interval. The slider can show both major tick marks and minor tick marks between them. The number of values between the tick marks is controlled with setMajorTickSpacing and setMinorTickSpacing.

For the keyboard keys used by this component in the standard Look and Feel (L&F) renditions, see the JSlider key assignments.

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. A future release of Swing will provide support for long term persistence.

See Also:
Serialized Form

Inner Class Summary
protected  class JSlider.AccessibleJSlider
          The class used to obtain the accessible role for this object.
 
Inner classes inherited from class javax.swing.JComponent
JComponent.AccessibleJComponent
 
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 Sliders 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
           
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 class javax.swing.JComponent
accessibleContext, listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
 
Fields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
Fields inherited from interface javax.swing.SwingConstants
BOTTOM, CENTER, EAST, HORIZONTAL, LEADING, LEFT, NORTH, NORTH_EAST, NORTH_WEST, 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 intitial 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 intitial value of 50.
JSlider(int min, int max)
          Creates a horizontal slider using the specified min and max with an intitial value of 50.
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 mimimum, maximum, and initial values.
 
Method Summary
 void addChangeListener(ChangeListener l)
          Adds a ChangeListener to the slider.
protected  ChangeListener createChangeListener()
          Subclasses that want to handle model ChangeEvents differently can override this method to return their own ChangeListener implementation.
 Hashtable createStandardLabels(int increment)
          Creates a hashtable that will draw text labels starting at the slider minimum using the increment specified.
 Hashtable createStandardLabels(int increment, int start)
          Creates a hashtable that will draw text labels starting at the start point specified using the increment specified.
protected  void fireStateChanged()
          Send a ChangeEvent, whose source is this Slider, to each listener.
 AccessibleContext getAccessibleContext()
          Get the AccessibleContext associated with this JComponent
 int getExtent()
          Returns the "extent" -- the range of values "covered" by the knob.
 boolean getInverted()
          Returns true if the value-range shown for the slider is reversed, with the maximum value at the left end of a horizontal slider or at the bottom of a vertical one.
 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.
 int getMinimum()
          Returns the minimum value supported by the slider.
 int getMinorTickSpacing()
          This method returns the minor tick spacing.
 BoundedRangeModel getModel()
          Returns data model that handles the sliders 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 sliders value.
 boolean getValueIsAdjusting()
          True if the slider knob is being dragged.
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 setInverted(boolean b)
          Specify true to reverse the value-range shown for the slider so that the maximum value is at the left end of a horizontal slider or at the bottom of a vertical one.
 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 models maximum property.
 void setMinimum(int minimum)
          Sets the models minimum property.
 void setMinorTickSpacing(int n)
          This method sets the minor tick spacing.
 void setModel(BoundedRangeModel newModel)
          Sets the model that handles the sliders three fundamental properties: minimum, maximum, value.
 void setOrientation(int orientation)
          Set the scrollbars orientation to either VERTICAL or 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 sliders current value.
 void setValueIsAdjusting(boolean b)
          Sets the models valueIsAdjusting property.
protected  void updateLabelUIs()
          Called internally to replace the label UIs with the latest versions from the UIFactory when the UIFactory notifies us via updateUI that the L&F has changed.
 void updateUI()
          Notification from the UIFactory that the L&F has changed.
 
Methods inherited from class javax.swing.JComponent
addAncestorListener, addNotify, addPropertyChangeListener, addPropertyChangeListener, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getAlignmentX, getAlignmentY, getAutoscrolls, getBorder, getBounds, getClientProperty, getComponentGraphics, getConditionForKeyStroke, getDebugGraphicsOptions, getGraphics, getHeight, getInsets, getInsets, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getVisibleRect, getWidth, getX, getY, grabFocus, hasFocus, isDoubleBuffered, isFocusCycleRoot, isFocusTraversable, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, processComponentKeyEvent, processFocusEvent, processKeyEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removePropertyChangeListener, removePropertyChangeListener, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setDebugGraphicsOptions, setDoubleBuffered, setEnabled, setFont, setForeground, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setUI, setVisible, unregisterKeyboardAction, update
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, addImpl, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getLayout, insets, invalidate, isAncestorOf, layout, list, list, locate, minimumSize, paintComponents, preferredSize, print, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setCursor, setLayout, validate, validateTree
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, disable, disableEvents, dispatchEvent, enable, enable, enableEvents, enableInputMethods, getBackground, getBounds, getColorModel, getComponentOrientation, getCursor, getDropTarget, getFont, getFontMetrics, getForeground, getInputContext, getInputMethodRequests, getLocale, getLocation, getLocationOnScreen, getName, getParent, getPeer, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hide, imageUpdate, inside, isDisplayable, isEnabled, isLightweight, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, printAll, processComponentEvent, processInputMethodEvent, processMouseEvent, remove, removeComponentListener, removeFocusListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setComponentOrientation, setDropTarget, setLocale, setLocation, setLocation, setName, setSize, setSize, show, show, size, toString, transferFocus
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

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.
See Also:
setSnapToTicks(boolean)

orientation

protected int orientation
See Also:
setOrientation(int)

changeListener

protected ChangeListener changeListener
The changeListener (no suffix) is the listener we add to the Sliders model. By default this listener 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()
Constructor Detail

JSlider

public JSlider()
Creates a horizontal slider with the range 0 to 100 and an intitial value of 50.

JSlider

public JSlider(int orientation)
Creates a slider using the specified orientation with the range 0 to 100 and an intitial value of 50.

JSlider

public JSlider(int min,
               int max)
Creates a horizontal slider using the specified min and max with an intitial value of 50.

JSlider

public JSlider(int min,
               int max,
               int value)
Creates a horizontal slider using the specified min, max and value.

JSlider

public JSlider(int orientation,
               int min,
               int max,
               int value)
Creates a slider with the specified orientation and the specified mimimum, maximum, and initial values.
Throws:
IllegalArgumentException - if orientation is not one of VERTICAL, HORIZONTAL
See Also:
setOrientation(int), setMinimum(int), setMaximum(int), setValue(int)

JSlider

public JSlider(BoundedRangeModel brm)
Creates a horizontal slider using the specified BoundedRangeModel.
Method Detail

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()
Notification from the UIFactory that the L&F has changed. Called to replace the UI with the latest version from the default UIFactory.
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 model ChangeEvents differently can override this method to return their own ChangeListener implementation. The default ChangeListener just forwards ChangeEvents to the ChangeListeners added directly to the slider.
See Also:
fireStateChanged()

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)

fireStateChanged

protected void fireStateChanged()
Send a ChangeEvent, whose source is this Slider, to each listener. This method method is called each time a ChangeEvent is received from the model.
See Also:
addChangeListener(javax.swing.event.ChangeListener), EventListenerList

getModel

public BoundedRangeModel getModel()
Returns data model that handles the sliders three fundamental properties: minimum, maximum, value.
See Also:
setModel(javax.swing.BoundedRangeModel)

setModel

public void setModel(BoundedRangeModel newModel)
Sets the model that handles the sliders three fundamental properties: minimum, maximum, value.
See Also:
getModel()

getValue

public int getValue()
Returns the sliders value.
Returns:
the models value property
See Also:
setValue(int)

setValue

public void setValue(int n)
Sets the sliders current value. This method just forwards the value to the model.
See Also:
getValue()

getMinimum

public int getMinimum()
Returns the minimum value supported by the slider.
Returns:
the value of the models minimum property
See Also:
setMinimum(int)

setMinimum

public void setMinimum(int minimum)
Sets the models minimum property.
See Also:
getMinimum(), BoundedRangeModel.setMinimum(int)

getMaximum

public int getMaximum()
Returns the maximum value supported by the slider.
Returns:
the value of the models maximum property
See Also:
setMaximum(int)

setMaximum

public void setMaximum(int maximum)
Sets the models maximum property.
See Also:
getMaximum(), BoundedRangeModel.setMaximum(int)

getValueIsAdjusting

public boolean getValueIsAdjusting()
True if the slider knob is being dragged.
Returns:
the value of the models valueIsAdjusting property
See Also:
setValueIsAdjusting(boolean)

setValueIsAdjusting

public void setValueIsAdjusting(boolean b)
Sets the models 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.
See Also:
getValueIsAdjusting(), BoundedRangeModel.setValueIsAdjusting(boolean)

getExtent

public int getExtent()
Returns the "extent" -- 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.
See Also:
getExtent(), BoundedRangeModel.setExtent(int)

getOrientation

public int getOrientation()
Return this slider's vertical or horizontal orientation.
Returns:
VERTICAL or HORIZONTAL
See Also:
setOrientation(int)

setOrientation

public void setOrientation(int orientation)
Set the scrollbars orientation to either VERTICAL or HORIZONTAL.
Throws:
IllegalArgumentException - if orientation is not one of VERTICAL, HORIZONTAL
See Also:
getOrientation()

getLabelTable

public Dictionary getLabelTable()
Returns the dictionary of what labels to draw at which values.
Returns:
the Dictionary containing labels and where to draw them

setLabelTable

public void setLabelTable(Dictionary labels)
Used to specify what label will be drawn at any given value. The key-value pairs are of this format: { Integer value, java.awt.Component label }
See Also:
createStandardLabels(int), getLabelTable()

updateLabelUIs

protected void updateLabelUIs()
Called internally to replace the label UIs with the latest versions from the UIFactory when the UIFactory notifies us via updateUI that the L&F has changed.
See Also:
JComponent.updateUI()

createStandardLabels

public Hashtable createStandardLabels(int increment)
Creates a hashtable that will draw text labels starting at the slider minimum using the increment specified. If you call createStandardLabels( 10 ) and the slider minimum is zero, then it will make labels for the values 0, 10, 20, 30, and so on.
See Also:
setLabelTable(java.util.Dictionary)

createStandardLabels

public Hashtable createStandardLabels(int increment,
                                      int start)
Creates a hashtable that will draw text labels starting at the start point specified using the increment specified. If you call createStandardLabels( 10, 2 ), then it will make labels for the values 2, 12, 22, 32, and so on.
See Also:
setLabelTable(java.util.Dictionary)

getInverted

public boolean getInverted()
Returns true if the value-range shown for the slider is reversed, with the maximum value at the left end of a horizontal slider or at the bottom of a vertical one.
Returns:
true if the slider values are reversed from their normal order

setInverted

public void setInverted(boolean b)
Specify true to reverse the value-range shown for the slider so that the maximum value is at the left end of a horizontal slider or at the bottom of a vertical one. Specify false to put the value range in the normal order.
Parameters:
b - true to reverse the slider values from their normal order

getMajorTickSpacing

public int getMajorTickSpacing()
This method returns the major tick spacing. The number that is returned represents the distance, measured in values, between each major tick mark. If you have a slider with a range from 0 to 50 and the major tick spacing is set to 10, you will get major ticks next to the following values: 0, 10, 20, 30, 40, 50.
Returns:
the number of values between major ticks
See Also:
setMajorTickSpacing(int)

setMajorTickSpacing

public void setMajorTickSpacing(int n)
This method sets the major tick spacing. The number that is passed-in represents the distance, measured in values, between each major tick mark. If you have a slider with a range from 0 to 50 and the major tick spacing is set to 10, you will get major ticks next to the following values: 0, 10, 20, 30, 40, 50.
See Also:
getMajorTickSpacing()

getMinorTickSpacing

public int getMinorTickSpacing()
This method returns the minor tick spacing. The number that is returned represents the distance, measured in values, between each minor tick mark. If you have a slider with a range from 0 to 50 and the minor tick spacing is set to 10, you will get minor ticks next to the following values: 0, 10, 20, 30, 40, 50.
Returns:
the number of values between minor ticks
See Also:
getMinorTickSpacing()

setMinorTickSpacing

public void setMinorTickSpacing(int n)
This method sets the minor tick spacing. The number that is passed-in represents the distance, measured in values, between each minor tick mark. If you have a slider with a range from 0 to 50 and the minor tick spacing is set to 10, you will get minor ticks next to the following values: 0, 10, 20, 30, 40, 50.
See Also:
getMinorTickSpacing()

getSnapToTicks

public 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.
Returns:
true if the value snaps to the nearest tick mark, else false
See Also:
setSnapToTicks(boolean)

setSnapToTicks

public 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.
Parameters:
b - true to snap the knob to the nearest tick mark
See Also:
getSnapToTicks()

getPaintTicks

public boolean getPaintTicks()
Tells if tick marks are to be painted.
Returns:
true if tick marks are painted, else false
See Also:
setPaintTicks(boolean)

setPaintTicks

public void setPaintTicks(boolean b)
Determines whether tick marks are painted on the slider.
See Also:
getPaintTicks()

getPaintTrack

public boolean getPaintTrack()
Tells if the track (area the slider slides in) is to be painted.
Returns:
true if track is painted, else false
See Also:
setPaintTrack(boolean)

setPaintTrack

public void setPaintTrack(boolean b)
Determines whether the track is painted on the slider.
See Also:
getPaintTrack()

getPaintLabels

public boolean getPaintLabels()
Tells if labels are to be painted.
Returns:
true if labels are painted, else false
See Also:
setPaintLabels(boolean)

setPaintLabels

public void setPaintLabels(boolean b)
Determines whether labels are painted on the slider.
See Also:
getPaintLabels()

paramString

protected String paramString()
Returns a string representation of this JSlider. This method is intended to be used only for debugging purposes, and the content and format of the returned string may vary between implementations. The returned string may be empty but may not be null.
Overrides:
paramString in class JComponent
Returns:
a string representation of this JSlider.

getAccessibleContext

public AccessibleContext getAccessibleContext()
Get the AccessibleContext associated with this JComponent
Specified by:
getAccessibleContext in interface Accessible
Overrides:
getAccessibleContext in class JComponent
Returns:
the AccessibleContext of this JComponent

JavaTM 2 Platform
Standard Edition

Submit a bug or feature
Java, Java 2D, and JDBC are a trademarks or registered trademarks of Sun Microsystems, Inc. in the US and other countries.
Copyright 1993-1999 Sun Microsystems, Inc. 901 San Antonio Road,
Palo Alto, California, 94303, U.S.A. All Rights Reserved.