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 knob is always positioned at the points that match integer values within the specified 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 JavaBeans™ has been added to the java.beans
package. Please see XMLEncoder
.
Modifier and Type | Class and Description |
---|---|
protected class |
JSlider.AccessibleJSlider This class implements accessibility support for the |
JComponent.AccessibleJComponent
Container.AccessibleAWTContainer
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy
protected BoundedRangeModel sliderModel
The data model that handles the numeric maximum value, minimum value, and current-position value for the slider.
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.
setMinorTickSpacing(int)
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.
setSnapToTicks(boolean)
protected int orientation
Whether the slider is horizontal or vertical The default is horizontal.
setOrientation(int)
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 ChangeListener
s (if any) added directly to the slider.
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.
fireStateChanged()
public JSlider()
Creates a horizontal slider with the range 0 to 100 and an initial value of 50.
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
.
orientation
- the orientation of the sliderIllegalArgumentException
- if orientation is not one of VERTICAL
, HORIZONTAL
setOrientation(int)
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.
min
- the minimum value of the slidermax
- the maximum value of the sliderBoundedRangeModel
, setMinimum(int)
, setMaximum(int)
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.
min
- the minimum value of the slidermax
- the maximum value of the slidervalue
- the initial value of the sliderBoundedRangeModel
, setMinimum(int)
, setMaximum(int)
, setValue(int)
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.
orientation
- the orientation of the slidermin
- the minimum value of the slidermax
- the maximum value of the slidervalue
- the initial value of the sliderIllegalArgumentException
- if orientation is not one of VERTICAL
, HORIZONTAL
BoundedRangeModel
, setOrientation(int)
, setMinimum(int)
, setMaximum(int)
, setValue(int)
public JSlider(BoundedRangeModel brm)
Creates a horizontal slider using the specified BoundedRangeModel.
public SliderUI getUI()
Gets the UI object which implements the L&F for this component.
public void setUI(SliderUI ui)
Sets the UI object which implements the L&F for this component.
ui
- the SliderUI L&F objectUIDefaults.getUI(javax.swing.JComponent)
public void updateUI()
Resets the UI property to a value from the current look and feel.
updateUI
in class JComponent
JComponent.updateUI()
public String getUIClassID()
Returns the name of the L&F class that renders this component.
getUIClassID
in class JComponent
JComponent.getUIClassID()
, UIDefaults.getUI(javax.swing.JComponent)
protected ChangeListener createChangeListener()
Subclasses that want to handle ChangeEvent
s 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 ChangeEvent
s to the ChangeListener
s that have been added directly to the slider.
changeListener
, fireStateChanged()
, ChangeListener
, BoundedRangeModel
public void addChangeListener(ChangeListener l)
Adds a ChangeListener to the slider.
l
- the ChangeListener to addfireStateChanged()
, removeChangeListener(javax.swing.event.ChangeListener)
public void removeChangeListener(ChangeListener l)
Removes a ChangeListener from the slider.
l
- the ChangeListener to removefireStateChanged()
, addChangeListener(javax.swing.event.ChangeListener)
public ChangeListener[] getChangeListeners()
Returns an array of all the ChangeListener
s added to this JSlider with addChangeListener().
ChangeListener
s added or an empty array if no listeners have been addedprotected void fireStateChanged()
Send a ChangeEvent
, whose source is this JSlider
, to all ChangeListener
s that have registered interest in ChangeEvent
s. This method is called each time a ChangeEvent
is received from the model.
The event instance is created if necessary, and stored in changeEvent
.
public BoundedRangeModel getModel()
Returns the BoundedRangeModel
that handles the slider's three fundamental properties: minimum, maximum, value.
setModel(javax.swing.BoundedRangeModel)
, BoundedRangeModel
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.
newModel
- the new, non-null
BoundedRangeModel
to usegetModel()
, BoundedRangeModel
public int getValue()
Returns the slider's current value from the BoundedRangeModel
.
setValue(int)
, BoundedRangeModel.getValue()
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.
n
- the new valuegetValue()
, addChangeListener(javax.swing.event.ChangeListener)
, BoundedRangeModel.setValue(int)
public int getMinimum()
Returns the minimum value supported by the slider from the BoundedRangeModel
.
setMinimum(int)
, BoundedRangeModel.getMinimum()
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.
minimum
- the new minimumgetMinimum()
, addChangeListener(javax.swing.event.ChangeListener)
, BoundedRangeModel.setMinimum(int)
public int getMaximum()
Returns the maximum value supported by the slider from the BoundedRangeModel
.
setMaximum(int)
, BoundedRangeModel.getMaximum()
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.
maximum
- the new maximumgetMaximum()
, addChangeListener(javax.swing.event.ChangeListener)
, BoundedRangeModel.setMaximum(int)
public boolean getValueIsAdjusting()
Returns the valueIsAdjusting
property from the model. For details on how this is used, see the setValueIsAdjusting
documentation.
valueIsAdjusting
propertysetValueIsAdjusting(boolean)
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.
b
- the new value for the valueIsAdjusting
propertygetValueIsAdjusting()
, BoundedRangeModel.setValueIsAdjusting(boolean)
public int getExtent()
Returns the "extent" from the BoundedRangeModel
. This represents the range of values "covered" by the knob.
setExtent(int)
, BoundedRangeModel.getExtent()
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
documentation for details.
If the new extent value is different from the previous extent value, all change listeners are notified.
extent
- the new extentgetExtent()
, BoundedRangeModel.setExtent(int)
public int getOrientation()
Return this slider's vertical or horizontal orientation.
SwingConstants.VERTICAL
or SwingConstants.HORIZONTAL
setOrientation(int)
public void setOrientation(int orientation)
Set the slider's orientation to either SwingConstants.VERTICAL
or SwingConstants.HORIZONTAL
.
orientation
- HORIZONTAL
or VERTICAL
IllegalArgumentException
- if orientation is not one of VERTICAL
, HORIZONTAL
getOrientation()
public void setFont(Font font)
Sets the font for this component.
setFont
in class JComponent
font
- the desired Font
for this componentComponent.getFont()
public boolean imageUpdate(Image img, int infoflags, int x, int y, int w, int h)
Repaints the component when the image has changed. This imageUpdate
method of an ImageObserver
is called when more information about an image which had been previously requested using an asynchronous routine such as the drawImage
method of Graphics
becomes available. See the definition of imageUpdate
for more information on this method and its arguments.
The imageUpdate
method of Component
incrementally draws an image on the component as more of the bits of the image are available.
If the system property awt.image.incrementaldraw
is missing or has the value true
, the image is incrementally drawn. If the system property has any other value, then the image is not drawn until it has been completely loaded.
Also, if incremental drawing is in effect, the value of the system property awt.image.redrawrate
is interpreted as an integer to give the maximum redraw rate, in milliseconds. If the system property is missing or cannot be interpreted as an integer, the redraw rate is once every 100ms.
The interpretation of the x
, y
, width
, and height
arguments depends on the value of the infoflags
argument.
imageUpdate
in interface ImageObserver
imageUpdate
in class Component
img
- the image being observedinfoflags
- see imageUpdate
for more informationx
- the x coordinatey
- the y coordinatew
- the widthh
- the heightfalse
if the infoflags indicate that the image is completely loaded; true
otherwise.ImageObserver
, Graphics.drawImage(Image, int, int, Color, java.awt.image.ImageObserver)
, Graphics.drawImage(Image, int, int, java.awt.image.ImageObserver)
, Graphics.drawImage(Image, int, int, int, int, Color, java.awt.image.ImageObserver)
, Graphics.drawImage(Image, int, int, int, int, java.awt.image.ImageObserver)
, ImageObserver.imageUpdate(java.awt.Image, int, int, int, int, int)
public Dictionary getLabelTable()
Returns the dictionary of what labels to draw at which values.
Dictionary
containing labels and where to draw thempublic 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.swing.JComponent label }
.
An easy way to generate a standard table of value labels is by using the createStandardLabels
method.
Once the labels have been set, this method calls updateLabelUIs()
. Note that the labels are only painted if the paintLabels
property is true
.
labels
- new Dictionary
of labels, or null
to remove all labelscreateStandardLabels(int)
, getLabelTable()
, setPaintLabels(boolean)
protected void updateLabelUIs()
Updates the UIs for the labels in the label table by calling updateUI
on each label. The UIs are updated from the current look and feel. The labels are also set to their preferred size.
public Hashtable createStandardLabels(int increment)
Creates a Hashtable
of numerical text labels, starting at the slider minimum, and using the increment specified. For example, if you call createStandardLabels( 10 )
and the slider minimum is zero, then labels will be created for the values 0, 10, 20, 30, and so on.
For the labels to be drawn on the slider, the returned Hashtable
must be passed into setLabelTable
, and setPaintLabels
must be set to true
.
For further details on the makeup of the returned Hashtable
, see the setLabelTable
documentation.
increment
- distance between labels in the generated hashtableHashtable
of labelsIllegalArgumentException
- if increment
is less than or equal to zerosetLabelTable(java.util.Dictionary)
, setPaintLabels(boolean)
public Hashtable createStandardLabels(int increment, int start)
Creates a Hashtable
of numerical text labels, starting at the starting point specified, and using the increment specified. For example, if you call createStandardLabels( 10, 2 )
, then labels will be created for the values 2, 12, 22, 32, and so on.
For the labels to be drawn on the slider, the returned Hashtable
must be passed into setLabelTable
, and setPaintLabels
must be set to true
.
For further details on the makeup of the returned Hashtable
, see the setLabelTable
documentation.
increment
- distance between labels in the generated hashtablestart
- value at which the labels will beginHashtable
of labelsIllegalArgumentException
- if start
is out of range, or if increment
is less than or equal to zerosetLabelTable(java.util.Dictionary)
, setPaintLabels(boolean)
public boolean getInverted()
Returns true if the value-range shown for the slider is reversed,
setInverted(boolean)
public 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. The order depends on the slider's ComponentOrientation
property. Normal (non-inverted) horizontal sliders with a ComponentOrientation
value of LEFT_TO_RIGHT
have their maximum on the right. Normal horizontal sliders with a ComponentOrientation
value of RIGHT_TO_LEFT
have their maximum on the left. Normal vertical sliders have their maximum on the top. These labels are reversed when the slider is inverted.
By default, the value of this property is false
.
b
- true to reverse the slider values from their normal orderpublic 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.
setMajorTickSpacing(int)
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.
In order for major ticks to be painted, setPaintTicks
must be set to true
.
This method will also set up a label table for you. If there is not already a label table, and the major tick spacing is > 0
, and getPaintLabels
returns true
, a standard label table will be generated (by calling createStandardLabels
) with labels at the major tick marks. For the example above, you would get text labels: "0", "10", "20", "30", "40", "50". The label table is then set on the slider by calling setLabelTable
.
n
- new value for the majorTickSpacing
propertygetMajorTickSpacing()
, setPaintTicks(boolean)
, setLabelTable(java.util.Dictionary)
, createStandardLabels(int)
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.
getMinorTickSpacing()
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.
In order for minor ticks to be painted, setPaintTicks
must be set to true
.
n
- new value for the minorTickSpacing
propertygetMinorTickSpacing()
, setPaintTicks(boolean)
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.
setSnapToTicks(boolean)
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. By default, this property is false
.
b
- true to snap the knob to the nearest tick markgetSnapToTicks()
public boolean getPaintTicks()
Tells if tick marks are to be painted.
setPaintTicks(boolean)
public void setPaintTicks(boolean b)
Determines whether tick marks are painted on the slider. By default, this property is false
.
b
- whether or not tick marks should be paintedgetPaintTicks()
public boolean getPaintTrack()
Tells if the track (area the slider slides in) is to be painted.
setPaintTrack(boolean)
public void setPaintTrack(boolean b)
Determines whether the track is painted on the slider. By default, this property is true
.
b
- whether or not to paint the slider trackgetPaintTrack()
public boolean getPaintLabels()
Tells if labels are to be painted.
setPaintLabels(boolean)
public void setPaintLabels(boolean b)
Determines whether labels are painted on the slider.
This method will also set up a label table for you. If there is not already a label table, and the major tick spacing is > 0
, a standard label table will be generated (by calling createStandardLabels
) with labels at the major tick marks. The label table is then set on the slider by calling setLabelTable
.
By default, this property is false
.
b
- whether or not to paint labelsgetPaintLabels()
, getLabelTable()
, createStandardLabels(int)
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
.
paramString
in class JComponent
public AccessibleContext getAccessibleContext()
Gets the AccessibleContext associated with this JSlider. For sliders, the AccessibleContext takes the form of an AccessibleJSlider. A new AccessibleJSlider instance is created if necessary.
getAccessibleContext
in interface Accessible
getAccessibleContext
in class Component
© 1993–2017, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.