The java.awt.TextArea
class is a subclass of
java.awt.TextComponent
that provides a widget for editing
multiple lines of text. It's useful for input and output.
There are five constructors:
public TextArea()
public TextArea(String text)
public TextArea(int rows, int columns)
public TextArea(String text, int rows, int columns)
public TextArea(String text, int rows, int columns, int scrollbars)
Because of the way Java lays out components, you should not use the
noargs constructor. Either start off with a String
or
specify the number of rows and columns this area is expected to
hold. For example,
TextArea address = new TextArea("Type your address here", 5, 80);
By default, TextAreas don't have scrollbars. However you can add them by passing one of these constants to the constructor:
TextArea.SCROLLBARS_BOTH
TextArea.SCROLLBARS_HORIZONTAL_ONLY
TextArea.SCROLLBARS_NONE
TextArea.SCROLLBARS_VERTICAL_ONLY
For example,
TextArea instructions =
new TextArea("", 15, 70, TextArea.SCROLLBARS_VERTICAL_ONLY);
Unlike text fields, text areas do not generate action events when the user hits return inside the area. Instead, the line is broken and the caret moves on to the next row.
However, the getText()
method does return the
contents of the TextArea
, and the
setText()
method does change it. The
setEditable()
method lets you determine whether or not, the
user can modify the contents of a TextField
. If these
methods sound familiar it's because they're both inherited from the
common superclass of TextField
and
TextArea
, TextComponent
.
Furthermore, you can append text to a TextArea
with
the append()
method, insert text into the middle of a
TextArea
with the insert()
method, and
replace a block of text with the replaceRange()
method:
public void insert(String text, int position)
public void append(String text)
public void replaceRange(String text, int start, int end)