- All Implemented Interfaces:
ImageObserver
,MenuContainer
,Serializable
,Accessible
,SwingConstants
@JavaBean(defaultProperty="UI", description="A component that supports selecting a integer value from a range.") public class JSlider extends JComponent implements SwingConstants, Accessible
The slider can show both
major tick marks, and minor tick marks between the major ones. The number of
values between the tick marks is controlled with
setMajorTickSpacing
and setMinorTickSpacing
.
Painting of tick marks is controlled by setPaintTicks
.
Sliders can also print text labels at regular intervals (or at
arbitrary locations) along the slider track. Painting of labels is
controlled by setLabelTable
and setPaintLabels
.
For further information and examples see How to Use Sliders, a section in The Java Tutorial.
Warning: Swing is not thread safe. For more information see Swing's Threading Policy.
Warning:
Serialized objects of this class will not be compatible with
future Swing releases. The current serialization support is
appropriate for short term storage or RMI between applications running
the same version of Swing. As of 1.4, support for long term storage
of all JavaBeans™
has been added to the java.beans
package.
Please see XMLEncoder
.
- Since:
- 1.2
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected class
JSlider.AccessibleJSlider
This class implements accessibility support for theJSlider
class.Nested classes/interfaces declared in class java.awt.Component
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy
-
Field Summary
Fields Modifier and Type Field Description protected ChangeEvent
changeEvent
Only oneChangeEvent
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 declared in class javax.swing.JComponent
listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
Fields declared in class java.awt.Component
accessibleContext, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
Fields declared in interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
Fields declared in 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
Constructors Constructor Description JSlider()
Creates a horizontal slider with the range 0 to 100 and an initial value of 50.JSlider(int orientation)
Creates a slider using the specified orientation with the range0
to100
and an initial value of50
.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.JSlider(BoundedRangeModel brm)
Creates a horizontal slider using the specified BoundedRangeModel. -
Method Summary
Modifier and Type Method Description void
addChangeListener(ChangeListener l)
Adds a ChangeListener to the slider.protected ChangeListener
createChangeListener()
Subclasses that want to handleChangeEvent
s from the model differently can override this to return an instance of a customChangeListener
implementation.Hashtable<Integer,JComponent>
createStandardLabels(int increment)
Creates aHashtable
of numerical text labels, starting at the slider minimum, and using the increment specified.Hashtable<Integer,JComponent>
createStandardLabels(int increment, int start)
Creates aHashtable
of numerical text labels, starting at the starting point specified, and using the increment specified.protected void
fireStateChanged()
Send aChangeEvent
, whose source is thisJSlider
, to allChangeListener
s that have registered interest inChangeEvent
s.AccessibleContext
getAccessibleContext()
Gets the AccessibleContext associated with this JSlider.ChangeListener[]
getChangeListeners()
Returns an array of all theChangeListener
s added to this JSlider with addChangeListener().int
getExtent()
Returns the "extent" from theBoundedRangeModel
.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 theBoundedRangeModel
.int
getMinimum()
Returns the minimum value supported by the slider from theBoundedRangeModel
.int
getMinorTickSpacing()
This method returns the minor tick spacing.BoundedRangeModel
getModel()
Returns theBoundedRangeModel
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 theBoundedRangeModel
.boolean
getValueIsAdjusting()
Returns thevalueIsAdjusting
property from the model.boolean
imageUpdate(Image img, int infoflags, int x, int y, int w, int h)
Repaints the component when the image has changed.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 tomaximum
.void
setMinimum(int minimum)
Sets the slider's minimum value tominimum
.void
setMinorTickSpacing(int n)
This method sets the minor tick spacing.void
setModel(BoundedRangeModel newModel)
Sets theBoundedRangeModel
that handles the slider's three fundamental properties: minimum, maximum, value.void
setOrientation(int orientation)
Set the slider's orientation to eitherSwingConstants.VERTICAL
orSwingConstants.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 ton
.void
setValueIsAdjusting(boolean b)
Sets the model'svalueIsAdjusting
property.protected void
updateLabelUIs()
Updates the UIs for the labels in the label table by callingupdateUI
on each label.void
updateUI()
Resets the UI property to a value from the current look and feel.Methods declared in class javax.swing.JComponent
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, 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, hide, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingOrigin, 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 declared in 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, transferFocusDownCycle, validate, validateTree
Methods declared in 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, 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, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list,