Java Reference
In-Depth Information
When text reaches the right edge of a
JTextArea
the text can wrap to the next line.
This is referred to as
line wrapping
. By default,
JTextArea
does
not
wrap lines.
Look-and-Feel Observation 12.19
To provide line wrapping functionality for a
JTextArea
, invoke
JTextArea
method
set-
LineWrap
with a
true
argument.
JScrollPane
Scrollbar Policies
This example uses a
JScrollPane
to provide scrolling for a
JTextArea
. By default,
JScrollPane
displays scrollbars
only
if they're required. You can set the horizontal and ver-
tical
scrollbar policies
of a
JScrollPane
when it's constructed. If a program has a reference
to a
JScrollPane
, the program can use
JScrollPane
methods
setHorizontal-
ScrollBarPolicy
and
setVerticalScrollBarPolicy
to change the scrollbar policies at
any time. Class
JScrollPane
declares the constants
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS
to indicate that
a scrollbar should always appear
, constants
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED
to indicate that
a scrollbar should appear only if necessary
(the defaults) and constants
JScrollPane.VERTICAL_SCROLLBAR_NEVER
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER
to indicate that
a scrollbar should never appear
. If the horizontal scrollbar policy is set to
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER
, a
JTextArea
attached to the
JScrollPane
will automatically wrap lines.
In this chapter, you learned many GUI components and how to handle their events. You
also learned about nested classes, inner classes and anonymous inner classes. You saw the
special relationship between an inner-class object and an object of its top-level class. You
learned how to use
JOptionPane
dialogs to obtain text input from the user and how to
display messages to the user. You also learned how to create applications that execute in
their own windows. We discussed class
JFrame
and components that enable a user to in-
teract with an application. We also showed you how to display text and images to the user.
You learned how to customize
JPanel
s to create custom drawing areas, which you'll use
extensively in the next chapter. You saw how to organize components on a window using
layout managers and how to creating more complex GUIs by using
JPanel
s to organize
components. Finally, you learned about the
JTextArea
component in which a user can
enter text and an application can display text. In Chapter 22, you'll learn about more ad-
vanced GUI components, such as sliders, menus and more complex layout managers. In
the next chapter, you'll learn how to add graphics to your GUI application. Graphics allow
you to draw shapes and text with colors and styles.