Java Reference
In-Depth Information
javax.swing.JComponent
The
get
and
set
methods for these data fields are provided
in the class, but omitted in the UML diagram for brevity.
javax.swing.JSlider
-maximum: int
-minimum: int
-value: int
-orientation: int
-paintLabels: boolean
-paintTicks: boolean
-paintTrack: boolean
-majorTickSpacing: int
-minorTickSpacing: int
-inverted: boolean
The maximum value represented by the slider (default:
100
).
The minimum value represented by the slider (default:
0
).
The current value represented by the slider.
The orientation of the slider (default:
JSlider.HORIZONTAL
).
True if the labels are painted at tick marks (default:
false
).
True if the ticks are painted on the slider (default:
false
).
True if the track is painted on the slider (default:
true
).
The number of units between major ticks (default:
0
).
The number of units between minor ticks (default:
0
).
True to reverse the value range, and false to put the value range in the
normal order (default:
false
).
Creates a default horizontal slider.
Creates a horizontal slider with the specified min and max.
Creates a horizontal slider with the specified min, max, and value.
Creates a slider with the specified orientation.
Creates a slider with the specified orientation, min, max, and value.
+JSlider()
+JSlider(min: int, max: int)
+JSlider(min: int, max: int, value: int)
+JSlider(orientation: int)
+JSlider(orientation: int, min: int, max:
int, value: int)
+addChangeListener(listener:
ChangeListener): void
Adds a
ChangeListener
to this object.
F
IGURE
17.14
JSlider
enables you to select from a range of values.
paintLabels
,
paintTicks
,
paintTrack
,
and
are
inverted
getPaintLabels()
,
getPaintTicks()
,
getPaintTrack()
,
and
getInverted()
, which violate the naming convention.
When the user changes the value of the slider, the slider fires an instance of
javax.swing.event.ChangeEvent
, which is passed to any registered listeners. Any
object that should be notified of changes to the slider's value must implement the
stateChanged
method in the
ChangeListener
interface defined in the package
javax.swing.event
.
The program in Listing 17.7 uses the sliders to control a message displayed on a panel, as
shown in Figure 17.14. Here are the major steps in the program:
1. Create the user interface.
Create a
MessagePanel
object and place it in the center of the frame. Create a vertical
slider and place it in the east of the frame. Create a horizontal slider and place it in the
south of the frame.
2. Process the event.
Create listeners to implement the
stateChanged
handler in the
ChangeListener
interface to move the message according to the knot movement in the slider.
L
ISTING
17.7
SliderDemo.java
1
import
java.awt.*;
2
import
javax.swing.*;
3
import
javax.swing.event.*;