is new.
java.lang.Objectjava.awt.Component
java.awt.Container
javax.swing.JComponent
javax.swing.JSlider
public class JSlider
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 them. The number of values between the tick marks is controlled with setMajorTickSpacing and setMinorTickSpacing.
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 TM 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. |
| Nested classes/interfaces inherited from class javax.swing. JComponent |
|---|
| JComponent.AccessibleJComponent |
| Nested classes/interfaces inherited from class java.awt. Container |
|---|
| Container.AccessibleAWTContainer |
| Nested classes/interfaces inherited from class java.awt. Component |
|---|
| Component.AccessibleAWTComponent , Component.BaselineResizeBehavior , Component.BltBufferStrategy , Component.FlipBufferStrategy |
| 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
|
| 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 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 , NEXT , NORTH , NORTH_EAST , NORTH_WEST , PREVIOUS , RIGHT , SOUTH , SOUTH_EAST , SOUTH_WEST , TOP , TRAILING , VERTICAL , WEST |
| Fields inherited from interface java.awt.image. ImageObserver |
|---|
| ABORT , ALLBITS , ERROR , FRAMEBITS , HEIGHT , PROPERTIES , SOMEBITS , WIDTH |
| 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
from the model
differently can override this
an instance of a custom
|
| Hashtable |
createStandardLabels
(int increment) Creates a
Hashtable of numerical
labels,
minimum, and
|
| Hashtable |
createStandardLabels
(int increment, int start) Creates a
Hashtable of numerical
labels,
starting
specified, and
|
| protected void |
fireStateChanged
() Send a ChangeEvent, whose source is this
JSlider,
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
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
slider's
|
| 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
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
value to maximum.
|
| void |
setMinimum
(int minimum) Sets the
slider's
value to minimum.
|
| void |
setMinorTickSpacing
(int n) This method sets the minor tick spacing. |
| void |
setModel
(
BoundedRangeModel
Sets the
BoundedRangeModel
slider's
|
| void |
setOrientation
(int orientation) Set the
slider's
SwingConstants.VERTICAL
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
value to n.
|
| void |
setValueIsAdjusting
(boolean b) Sets the
model's
|
| protected void |
updateLabelUIs
()
Updates
UIs for
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 java.lang. Object |
|---|
| clone , equals , finalize , getClass , hashCode , notify , notifyAll , wait , wait , wait |
| Field Detail |
|---|
protected BoundedRangeModel sliderModel
protected int majorTickSpacing
protected int minorTickSpacing
protected boolean snapToTicks
protected int orientation
Whether the slider is horizontal or vertical The default is horizontal.
protected ChangeListener changeListener
slider's
This
is initialized to the ChangeListener returned from createChangeListener, which by default
just forwards events to ChangeListeners (if any) added directly to the slider.
protected transient ChangeEvent changeEvent
| Constructor Detail |
|---|
public JSlider()
public JSlider(int orientation)
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)
public JSlider(int min,
int 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 slider
max - the maximum value of the slider
See Also:
BoundedRangeModel
,
setMinimum(int)
,
setMaximum(int)
public JSlider(int min,
int max,
int 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 slider
max - the maximum value of the slider
value - the initial value of the slider
See Also:
BoundedRangeModel
,
setMinimum(int)
,
setMaximum(int)
,
setValue(int)
public JSlider(int orientation,
int min,
int max,
int value)
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 slider
min - the minimum value of the slider
max - the maximum value of the slider
value - the initial value of the slider
BoundedRangeModel
,
setOrientation(int)
,
setMinimum(int)
,
setMaximum(int)
,
setValue(int)
public JSlider(BoundedRangeModel brm)
| Method Detail |
|---|
public SliderUI getUI()
public void setUI(SliderUI ui)
public void updateUI()
public String getUIClassID()
protected ChangeListener createChangeListener()
from the model
differently can override this
an instance of a custom
simply calls the fireStateChanged method to forward
that have been
added directly to the slider.
changeListener
,
fireStateChanged()
,
ChangeListener
,
BoundedRangeModel
public void addChangeListener(ChangeListener l)
public void removeChangeListener(ChangeListener l)
public ChangeListener[] getChangeListeners()
protected void fireStateChanged()
JSlider,
all ChangeListeners that have registered interest in ChangeEvents.
The event instance is created if necessary, and stored in changeEvent.
public BoundedRangeModel getModel()
the BoundedRangeModel
slider's
Returns:
the data model for this component
,
BoundedRangeModel
public void setModel(BoundedRangeModel newModel)
BoundedRangeModel
slider's
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
,
BoundedRangeModel
public int getValue()
Returns the slider's current value from the BoundedRangeModel.
the current value of the slider
,
BoundedRangeModel.getValue()
public void setValue(int n)
slider's
value to n.
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
,
addChangeListener(javax.swing.event.ChangeListener)
,
BoundedRangeModel.setValue(int)
public int getMinimum()
slider from the BoundedRangeModel.
model's
,
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.
Parameters:
n - the new minimum
,
addChangeListener(javax.swing.event.ChangeListener)
,
BoundedRangeModel.setMinimum(int)
public int getMaximum()
slider from the BoundedRangeModel.
model's
,
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.
Parameters:
n - the new maximum
,
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.
model's
public void setValueIsAdjusting(boolean b)
model's
Parameters:
b - the new value for the valueIsAdjusting property
public int getExtent()
from
BoundedRangeModel. This respresents the
range of values "covered" by the knob.
public void setExtent(int extent)
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.
Parameters:
extent - the new extent
public int getOrientation()
SwingConstants.VERTICAL or SwingConstants.HORIZONTAL
public void setOrientation(int orientation)
slider's
SwingConstants.VERTICAL
SwingConstants.HORIZONTAL.
Parameters:
orientation - HORIZONTAL or VERTICAL
public void setFont(Font font)
public Dictionary getLabelTable()
public void setLabelTable(Dictionary labels)
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.
Parameters:
labels - new Dictionary of labels, or null to remove all labels
,
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.
setLabelTable(java.util.Dictionary)
,
JComponent.updateUI()
public Hashtable createStandardLabels(int increment)
Hashtable of numerical
labels,
minimum, and
For example, if
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.
Parameters:
increment - distance between labels in the generated hashtable
Returns:
a new Hashtable of labels
Throws:
IllegalArgumentException
- if increment is less than or equal to zero
,
setPaintLabels(boolean)
public Hashtable createStandardLabels(int increment,
int start)
Hashtable of numerical
labels,
starting
specified, and
For example, if
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.
Parameters:
increment - distance between labels in the generated hashtable
start - value at which the labels will begin
Returns:
a new Hashtable of labels
is
range,
,
setPaintLabels(boolean)
public boolean getInverted()
public void setInverted(boolean b)
By default, the value of this property is false.
public int getMajorTickSpacing()
public void setMajorTickSpacing(int n)
passed in
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.
Parameters:
n - new value for the majorTickSpacing property
,
setPaintTicks(boolean)
,
setLabelTable(java.util.Dictionary)
,
createStandardLabels(int)
public int getMinorTickSpacing()
public void setMinorTickSpacing(int n)
passed in
In order for minor ticks to be painted, setPaintTicks must be set to true.
Parameters:
n - new value for the minorTickSpacing property
,
setPaintTicks(boolean)
public boolean getSnapToTicks()
public void setSnapToTicks(boolean b)
By default, this property is false.
public boolean getPaintTicks()
public void setPaintTicks(boolean b)
By default, this property is false.
Parameters:
b - whether or not tick marks should be painted
public boolean getPaintTrack()
public void setPaintTrack(boolean b)
By default, this property is true.
Parameters:
b - whether or not to paint the slider track
public boolean getPaintLabels()
public void setPaintLabels(boolean b)
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.
Parameters:
b - whether or not to paint labels
,
getLabelTable()
,
createStandardLabels(int)
protected String paramString()
public AccessibleContext getAccessibleContext()