Java Reference
In-Depth Information
public JSlider()
JSlider aJSlider = new JSlider();
public JSlider(int orientation)
// Vertical
JSlider aJSlider = new JSlider(JSlider.VERTICAL);
// Horizontal
JSlider bJSlider = new JSlider(JSlider.HORIZONTAL);
public JSlider(int minimum, int maximum)
// Initial value midpoint / 0
JSlider aJSlider = new JSlider(-100, 100);
public JSlider(int minimum, int maximum, int value)
JSlider aJSlider = new JSlider(-100, 100, 0);
public JSlider(int orientation, int minimum, int maximum, int value)
// Vertical, initial value 6, range 1-12 (months of year)
JSlider aJSlider = new JSlider(JSlider.VERTICAL, 6, 1, 12);
public JSlider(BoundedRangeModel model)
// Data model, initial value 3, range 1-31, and extent of 0
// JSlider direction changed to vertical prior to display on screen
DefaultBoundedRangeModel model = new DefaultBoundedRangeModel(3, 0, 1, 31);
JSlider aJSlider = new JSlider(model);
aJSlider.setOrientation(JSlider.VERTICAL);
Creating a
JSlider
with no arguments creates a horizontal slider with a default data model.
The model has an initial value of 50, a minimum of 0, a maximum of 100, and an extent of 0.
You can also explicitly set the orientation with
JSlider.HORIZONTAL
or
JSlider.VERTICAL
, and
any of the specific model properties, with the various constructors. In addition, you can explicitly
set the data model for the component.
If you're using a preconfigured
BoundedRangeModel
, remember to set the extent to 0 when
creating the model. If the
extent
property is greater than 0, then the maximum setting of the
value
property is decreased by that amount, and the
value
setting will never reach the setting
of the
maximum
property.
■
Caution
Initializing the orientation to something not equivalent to
VERTICAL
or
HORIZONTAL
throws
an
IllegalArgumentException
. All constructors that initialize the data model could throw an
IllegalArgumentException
if the range and initial value fail to abide by the rules of the
BoundedRangeModel
described earlier in the section “BoundedRangeModel Interface.”