Java Reference
In-Depth Information
}
});
JButton sb
;
p
.
add
(
sb
=
new
new
JButton
(
"Save Log"
));
sb
.
setEnabled
(
false
false
);
results
=
new
new
JTextArea
(
20
,
60
);
// Add the text area to the main part of the window (CENTER).
// Wrap it in a JScrollPane to make it scroll automatically.
cp
.
add
(
BorderLayout
.
CENTER
,
new
new
JScrollPane
(
results
));
setDefaultCloseOperation
(
JFrame
.
EXIT_ON_CLOSE
);
pack
();
// end of GUI portion
out
=
new
new
PrintStream
(
new
new
TextAreaOutputStream
(
results
));
}
}
Choosing a Value with JSpinner
Problem
You want to let the user choose from a fixed set of values, but do not want to use a
JList
or
JComboBox
because they take up too much “screen real estate.”
Solution
Use a
JSpinner
.
Discussion
The
JSpinner
class lets the user click up or down to cycle through a set of values. The val-
ues can be of any type because they are managed by a helper of type
SpinnerModel
and dis-
played by another helper of type
SpinnerEditor
. A series of predefined
SpinnerModels
handle
Number
s,
Date
s, and
List
s (which can be arrays or
Collections
). A demonstration
program is listed in
Example 14-8
;
its output is shown in
Figure 14-9
.