Java Reference
In-Depth Information
Display 18.6
View Port for a Text Area
Text area
When using scroll bars, the text is viewed through a
view
port
that shows only part of the text at a time. You can
view a different part of the text by using the scroll bars
that are placed along the sides of the view port. It is as if
the text were written on an unbounded sheet of paper,
but the paper is covered by another piece of paper with a
rectangular cutout that lets you see a portion of the text.
The cutout is the view port. This is illustrated in Display
18.4. You then use the scroll bars to move the view port
so that different portions of the text can be seen through
the cut-out view port. (You may prefer to think of the
view port as fixed and the text as moving. These two
ways of thinking are equivalent.) Swing allows you to
add scroll bars to your text areas by using the class
JScrollPane
.
An object of the class
JScrollPane
is essentially a view
port with scroll bars. When you create a
JScrollPane
,
View port
Display 18.7
Some Methods in the Class
JScrollPane
(part 1 of 2)
The
JScrollPane
class is in the
javax.swing
package.
public
JScrollPane(Component objectToBeScrolled)
Creates a new
JScrollPane
for the
objectToBeScrolled
. Note that the
objectToBeScrolled
need not be a
JTextArea
, although that is the only type of argument considered in this topic.
public
void
setHorizontalScrollBarPolicy(
int
policy)
Sets the policy for showing the horizontal scroll bar. The
policy
should be one of
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED
The phrase
AS_NEEDED
means the scroll bar is shown only when it is needed. This is explained more
fully in the text. The meanings of the other policy constants are obvious from their names.
(As indicated, these constants are defined in the class
JScrollPane
. You should not need to even
be aware of the fact that they have
int
values. Think of them as policies, not as
int
values.)