Java Reference
In-Depth Information
Scroll Bars
The class
JScrollPane
is used to add scroll bars to a
JTextArea
(and certain other compo-
nents). The
JTextArea
object is given as an argument to the constructor that creates the
JScrollPane
. The
JScrollPane
class is in the
javax.swing
package.
SYNTAX
JScrollPane
Identifier
=
new
JScrollPane(
Text_Area_Object
);
EXAMPLES
JTextArea memoDisplay =
new
JTextArea(LINES, CHAR_PER_LINE);
JScrollPane scrolledText =
new
JScrollPane(memoDisplay);
textPanel.add(scrolledText);
Self-Test Exercises
8. When setting up a
JScrollPane
, do you have to invoke both of the methods
setHorizontalScrollBarPolicy
and
setVerticalScrollBarPolicy
?
9. In Display 18.7, we listed the constructor for
JScrollPane
as follows:
public
JScrollPane(Component objectToBeScrolled)
This indicates that the argument to the constructor must be of type
Component
.
But we used the constructor with an argument of type
JTextArea
. Isn't this some
sort of type violation?
EXAMPLE:
Components with Changing Visibility
The GUI in Display 18.9 has labels that change from visible to invisible and back
again. Since the labels contain nothing but an icon each, it appears as if the icons
change roles from visible to invisible and back again. When the GUI is first run, the
label with Duke not waving is shown. When the
"Wave"
button is clicked, the label
with Duke not waving disappears and the label with Duke waving appears. When the
button labeled
"Stop"
is clicked, the label with Duke waving disappears and the label
with Duke not waving returns. Note that you can make a component invisible with-
out making the entire GUI invisible.
(continued)