| |||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
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 {@code setPaintTicks}.
Sliders can also print text labels at regular intervals (or at arbitrary locations) along the slider track. Painting of labels is controlled by {@code setLabelTable} and {@code 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 {@link java.beans.XMLEncoder}.
Nested Class Summary | |
---|---|
protected class |
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 transient 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 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 |
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 to and an initial value of . |
|
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 |
Adds a ChangeListener to the slider. |
protected ChangeListener |
Subclasses that want to handle s from the model differently can override this to return an instance of a custom ChangeListener implementation.
|
Hashtable |
createStandardLabels(int increment) Creates a of numerical text labels, starting at the slider minimum, and using the increment specified. |
Hashtable |
createStandardLabels(int increment, int start) Creates a of numerical text labels, starting at the starting point specified, and using the increment specified. |
protected void |
Send a , whose source is this , to all s that have registered interest in s. |
AccessibleContext |
Gets the AccessibleContext associated with this JSlider. |
ChangeListener[] |
Returns an array of all the ChangeListener s added
to this JSlider with addChangeListener().
|
int |
Returns the "extent" from the BoundedRangeModel .
|
boolean |
Returns true if the value-range shown for the slider is reversed, |
Dictionary |
Returns the dictionary of what labels to draw at which values. |
int |
This method returns the major tick spacing. |
int |
Returns the maximum value supported by the slider from the BoundedRangeModel .
|
int |
Returns the minimum value supported by the slider from the BoundedRangeModel .
|
int |
This method returns the minor tick spacing. |
BoundedRangeModel |
getModel() Returns the that handles the slider's three fundamental properties: minimum, maximum, value. |
int |
Return this slider's vertical or horizontal orientation. |
boolean |
Tells if labels are to be painted. |
boolean |
Tells if tick marks are to be painted. |
boolean |
Tells if the track (area the slider slides in) is to be painted. |
boolean |
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 |
Returns the name of the L&F class that renders this component. |
int |
getValue() Returns the slider's current value from the . |
boolean |
Returns the property from the model. |
protected String |
Returns a string representation of this JSlider. |
void |
Removes a ChangeListener from the slider. |
void |
setExtent(int extent) Sets the size of the range "covered" by the knob. |
void |
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 . |
void |
setMinimum(int minimum) Sets the slider's minimum value to . |
void |
setMinorTickSpacing(int n) This method sets the minor tick spacing. |
void |
setModel(BoundedRangeModel newModel) Sets the that handles the slider's three fundamental properties: minimum, maximum, value. |
void |
setOrientation(int orientation) Set the slider's orientation to either or . |
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 |
Sets the UI object which implements the L&F for this component. |
void |
setValue(int n) Sets the slider's current value to . |
void |
setValueIsAdjusting(boolean b) Sets the model's property. |
protected void |
Updates the UIs for the labels in the label table by calling 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, toString, wait, wait, wait |
Field Detail |
---|
protected transient ChangeEvent changeEvent
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.
protected ChangeListener changeListener
protected int majorTickSpacing
protected int minorTickSpacing
protected int orientation
protected BoundedRangeModel sliderModel
protected boolean snapToTicks
Constructor Detail |
---|
public JSlider()
public JSlider(BoundedRangeModel brm)
brm
public JSlider(int orientation)
SwingConstants.VERTICAL
or
SwingConstants.HORIZONTAL
.
orientation
- the orientation of the sliderpublic 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
{@code BoundedRangeModel} documentation for details.
min
- the minimum value of the slidermax
- the maximum value of the sliderpublic 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
{@code BoundedRangeModel} documentation for details.
min
- the minimum value of the slidermax
- the maximum value of the slidervalue
- the initial value of the sliderpublic JSlider(int orientation, int min, int max, int value)
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
{@code 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 sliderMethod Detail |
---|
public void addChangeListener(ChangeListener l)
l
- the ChangeListener to addprotected ChangeListener createChangeListener()
ChangeListener
implementation.
The default {@code ChangeListener} simply calls the
{@code fireStateChanged} method to forward {@code ChangeEvent}s
to the {@code ChangeListener}s that have been added directly to the
slider.
public Hashtable createStandardLabels(int increment)
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 {@code Hashtable} must be passed into {@code setLabelTable}, and {@code setPaintLabels} must be set to {@code true}.
For further details on the makeup of the returned {@code Hashtable}, see the {@code setLabelTable} documentation.
increment
- distance between labels in the generated hashtablepublic Hashtable createStandardLabels(int increment, int start)
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 {@code Hashtable} must be passed into {@code setLabelTable}, and {@code setPaintLabels} must be set to {@code true}.
For further details on the makeup of the returned {@code Hashtable}, see the {@code setLabelTable} documentation.
increment
- distance between labels in the generated hashtablestart
- value at which the labels will beginprotected void fireStateChanged()
The event instance is created if necessary, and stored in {@code changeEvent}.
public AccessibleContext getAccessibleContext()
getAccessibleContext
in class JComponent
public ChangeListener[] getChangeListeners()
ChangeListener
s added
to this JSlider with addChangeListener().
ChangeListener
s added or an empty
array if no listeners have been addedpublic int getExtent()
BoundedRangeModel
.
This respresents the range of values "covered" by the knob.
public boolean getInverted()
public Dictionary getLabelTable()
Dictionary
containing labels and
where to draw thempublic int getMajorTickSpacing()
public int getMaximum()
BoundedRangeModel
.
public int getMinimum()
BoundedRangeModel
.
public int getMinorTickSpacing()
public BoundedRangeModel getModel()
public int getOrientation()
public boolean getPaintLabels()
public boolean getPaintTicks()
public boolean getPaintTrack()
public boolean getSnapToTicks()
public SliderUI getUI()
public String getUIClassID()
getUIClassID
in class JComponent
public int getValue()
public boolean getValueIsAdjusting()
protected String paramString()
null
.
paramString
in class JComponent
public void removeChangeListener(ChangeListener l)
l
- the ChangeListener to removepublic void setExtent(int extent)
The data model (an instance of {@code BoundedRangeModel}) handles any mathematical issues arising from assigning faulty values. See the {@code BoundedRangeModel} documentation for details.
If the new extent value is different from the previous extent value, all change listeners are notified.
extent
- the new extentpublic void setFont(Font font)
setFont
in class JComponent
font
public void setInverted(boolean b)
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 {@code false}.
b
- true to reverse the slider values from their normal orderpublic void setLabelTable(Dictionary labels)
{ Integer value, java.swing.JComponent label }
.
An easy way to generate a standard table of value labels is by using the {@code createStandardLabels} method.
Once the labels have been set, this method calls {@link #updateLabelUIs}. Note that the labels are only painted if the {@code paintLabels} property is {@code true}.
labels
- new {@code Dictionary} of labels, or {@code null} to
remove all labelspublic void setMajorTickSpacing(int n)
In order for major ticks to be painted, {@code setPaintTicks} must be set to {@code 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 {@code > 0}, and {@code getPaintLabels} returns {@code true}, a standard label table will be generated (by calling {@code 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 {@code setLabelTable}.
n
- new value for the {@code majorTickSpacing} propertypublic void setMaximum(int maximum)
The data model (an instance of {@code BoundedRangeModel}) handles any mathematical issues arising from assigning faulty values. See the {@code BoundedRangeModel} documentation for details.
If the new maximum value is different from the previous maximum value, all change listeners are notified.
maximum
public void setMinimum(int minimum)
The data model (an instance of {@code BoundedRangeModel}) handles any mathematical issues arising from assigning faulty values. See the {@code BoundedRangeModel} documentation for details.
If the new minimum value is different from the previous minimum value, all change listeners are notified.
minimum
public void setMinorTickSpacing(int n)
In order for minor ticks to be painted, {@code setPaintTicks} must be set to {@code true}.
n
- new value for the {@code minorTickSpacing} propertypublic void setModel(BoundedRangeModel newModel)
Attempts to pass a {@code null} model to this method result in undefined behavior, and, most likely, exceptions.
newModel
- the new, {@code non-null} BoundedRangeModel
to usepublic void setOrientation(int orientation)
orientation
- {@code HORIZONTAL} or {@code VERTICAL}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 {@code > 0}, a standard label table will be generated (by calling {@code createStandardLabels}) with labels at the major tick marks. The label table is then set on the slider by calling {@code setLabelTable}.
By default, this property is {@code false}.
b
- whether or not to paint labelspublic void setPaintTicks(boolean b)
b
- whether or not tick marks should be paintedpublic void setPaintTrack(boolean b)
b
- whether or not to paint the slider trackpublic void setSnapToTicks(boolean b)
b
- true to snap the knob to the nearest tick markpublic void setUI(SliderUI ui)
ui
- the SliderUI L&F objectpublic void setValue(int n)
The data model (an instance of {@code BoundedRangeModel}) handles any mathematical issues arising from assigning faulty values. See the {@code BoundedRangeModel} documentation for details.
If the new value is different from the previous value, all change listeners are notified.
n
- the new valuepublic void setValueIsAdjusting(boolean b)
b
- the new value for the {@code valueIsAdjusting} propertyprotected void updateLabelUIs()
public void updateUI()
updateUI
in class JComponent
| |||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |