Java Reference
In-Depth Information
When the user drags the bubble, or clicks the increment or decrement unit, the value of the
scroll bar changes. An instance of
AdjustmentEvent
is fired and passed to the listener by
invoking the
adjustmentValueChanged
handler. The listener for the vertical scroll bar
moves the message up and down (lines 33-44), and the listener for the horizontal bar moves
the message to the right and left (lines 45-56).
The maximum value of the vertical scroll bar corresponds to the height of the panel, and the
maximum value of the horizontal scroll bar corresponds to the width of the panel. The ratio
between the current and maximum values of the horizontal scroll bar is the same as the ratio
between the
x
value and the width of the message panel. Similarly, the ratio between the
current and maximum values of the vertical scroll bar is the same as the ratio between the
y
value and the height of the message panel. The
x
-coordinate and
y
-coordinate are set in
response to the scroll bar adjustments (lines 39, 50).
17.17
How do you create a horizontal scroll bar? How do you create a vertical scroll bar?
17.18
What event can a scroll bar fire when the user changes the value on a scroll bar?
What is the corresponding interface for the event? What is the handler defined in the
interface?
17.19
How do you get the value from a scroll bar? How do you get the maximum value
from a scroll bar?
✓
✓
Check
Point
JSlider
is similar to
JScrollBar
, but
JSlider
has more properties and can
appear in many forms.
Key
Point
Figure 17.13 shows two sliders.
JSlider
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 pixels between the tick marks is controlled by the
majorTickSpacing
and
minorTickSpacing
properties. Sliders can be displayed horizon-
tally and/or vertically, with or without ticks, and with or without labels.
MessagePanel
Vertical slider
Horizontal slider
F
IGURE
17.13
The sliders move the message on a panel horizontally and vertically.
The frequently used constructors and properties in
JSlider
are shown in Figure 17.14.
Note
The values of a vertical scroll bar increase from top to bottom, but the values of a verti-
cal slider decrease from top to bottom by default.
Note
All the properties listed in Figure 17.14 have the associated
get
and
set
methods, but
they are omitted for brevity. By convention, the
get
method for a Boolean property is
named
is<PropertyName>()
. In the
JSlider
class, the
get
methods for