Java Reference
In-Depth Information
By default, the selection mode of the list is multiple-interval, which allows the user to
select multiple items from different blocks in the list. When the user selects countries in the
list, the
valueChanged
handler (lines 58-72) is executed, which gets the indices of the
selected items and sets their corresponding image icons in the label to display the flags.
17.13
How do you create a list with an array of strings?
17.14
How do you set the visible row count in a list?
17.15
What selection modes are available for a list? How do you set a selection mode?
17.16
How do you set the foreground and background color of a list? How do you set the
foreground and background color of the selected items?
✓
✓
Check
Point
JScrollBar
is a component that enables the user to select from a range of values.
Key
Point
Figure 17.10 shows a scroll bar. Normally, the user changes the value of the scroll bar by mak-
ing a gesture with the mouse. For example, the user can drag the scroll bar's bubble up and
down, or click in the scroll bar's unit-increment or block-increment areas. Keyboard gestures
can also be mapped to the scroll bar. By convention, the
Page Up
and
Page Down
keys are
equivalent to clicking in the scroll bar's block-increment and block-decrement areas.
Minimum value
Maximum value
Block decrement
Block increment
Bubble
Unit decrement
Unit increment
F
IGURE
17.10
A scroll bar represents a range of values graphically.
Note
The width of the scroll bar's track corresponds to
maximum + visibleAmount
.
When a scroll bar is set to its maximum value, the left side of the bubble is at
maximum
,
and the right side is at
maximum + visibleAmount
.
JScrollBar
has the following properties, as shown in Figure 17.11.
When the user changes the value of the scroll bar, the scroll bar fires an
AdjustmentEvent
.
A listener class for this event must implement the
adjustmentValueChanged
handler in the
java.awt.event.AdjustmentListener
interface.
Listing 17.6 gives a program that uses horizontal and vertical scroll bars to control a mes-
sage displayed on a panel. The horizontal scroll bar is used to move the message to the left
and the right, and the vertical scroll bar to move it up and down. A sample run of the program
is shown in Figure 17.12.
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
scroll bar and place it in the east of the frame. Create a horizontal scroll bar and place it
in the south of the frame.