A TextArea is a multi-line area that displays plain text.
It is intended to be a lightweight component that provides source
compatibility with the java.awt.TextArea class where it can
reasonably do so.
You can find information and examples of using all the text components in
Using Text Components,
a section in The Java Tutorial.
This component has capabilities not found in
the java.awt.TextArea class. The superclass should be consulted for
additional capabilities. Alternative multi-line text classes with
more capabilitites are JTextPane and JEditorPane.
The java.awt.TextArea internally handles scrolling.
JTextArea is different in that it doesn't manage scrolling,
but implements the swing Scrollable interface. This allows it
to be placed inside a JScrollPane if scrolling behavior is desired,
and used directly if scrolling is not desired.
The java.awt.TextArea has the ability to do line wrapping.
This was controlled by the horizontal scrolling policy. Since
scrolling is not done by JTextArea directly, backward
compatibility must be provided another way. JTextArea has
a bound property for line wrapping that controls whether or
not it will wrap lines. By default, the line wrapping property
is set to false (not wrapped).
java.awt.TextArea has two properties rows
and columns that are used to determine the preferred size.
JTextArea uses these properties to indicate the
preferred size of the viewport when placed inside a JScrollPane to
match the functionality provided by java.awt.TextArea.
JTextArea has a preferred size of what is needed to
display all of the text, so that it functions properly inside of
a JScrollPane. If the value for the rows or columns
is equal to zero, the preferred size along that axis is used for
the viewport preferred size along the same axis.
The java.awt.TextArea could be monitored for changes by adding
a TextListener for TextEvent's. In the JTextComponent based
components, changes are broadcasted from the model via a
DocumentEvent to DocumentListeners. The DocumentEvent gives
the location of the change and the kind of change if desired.
The code fragment might look something like:
For the keyboard keys used by this component in the standard Look and
Feel (L&F) renditions, see the
JTextArea key assignments.
Warning:
Serialized objects of this class will not be compatible with
future Swing releases. The current serialization support is appropriate
for short term storage or RMI between applications running the same
version of Swing. A future release of Swing will provide support for
long term persistence.
JTextArea(Document doc)
Constructs a new JTextArea with the given document model, and defaults
for all of the other arguments (null, 0, 0).
JTextArea(Document doc,
String text,
int rows,
int columns)
Constructs a new JTextArea with the specified number of rows
and columns, and the given model.
JTextArea(int rows,
int columns)
Constructs a new empty TextArea with the specified number of
rows and columns.
JTextArea(String text)
Constructs a new TextArea with the specified text displayed.
JTextArea(String text,
int rows,
int columns)
Constructs a new TextArea with the specified text and number
of rows and columns.
Method Summary
void
append(String str)
Appends the given text to the end of the document.
getRowHeight()
Defines the meaning of the height of a row.
int
getRows()
Returns the number of rows in the TextArea.
boolean
getScrollableTracksViewportWidth()
Returns true if a viewport should always force the width of this
Scrollable to match the width of the viewport.
int
getScrollableUnitIncrement(Rectangle visibleRect,
int orientation,
int direction)
Components that display logical rows or columns should compute
the scroll increment that will completely expose one new row
or column, depending on the value of orientation.
int
getTabSize()
Gets the number of characters used to expand tabs.
Creates the default implementation of the model
to be used at construction if one isn't explicitly
given. A new instance of PlainDocument is returned.
Returns:
the default document model
setTabSize
public void setTabSize(int size)
Sets the number of characters to expand tabs to.
This will be multiplied by the maximum advance for
variable width fonts. A PropertyChange event ("tabSize") is fired
when the tab size changes.
Gets the number of characters used to expand tabs. If the document is
null or doesn't have a tab setting, return a default of 8.
Returns:
the number of characters
setLineWrap
public void setLineWrap(boolean wrap)
Sets the line-wrapping policy of the text area. If set
to true the lines will be wrapped if they are too long
to fit within the allocated width. If set to false,
the lines will always be unwrapped. A PropertyChange event ("lineWrap")
is fired when the policy is changed. By default this property
is false.
Gets the line-wrapping policy of the text area. If set
to true the lines will be wrapped if they are too long
to fit within the allocated width. If set to false,
the lines will always be unwrapped.
setWrapStyleWord
public void setWrapStyleWord(boolean word)
Set the style of wrapping used if the text area is wrapping
lines. If set to true the lines will be wrapped at word
boundaries (whitespace) if they are too long
to fit within the allocated width. If set to false,
the lines will be wrapped at character boundaries.
By default this property is false.
Parameters:
word - indicates if word boundaries should be used
for line wrapping.
Get the style of wrapping used if the text area is wrapping
lines. If set to true the lines will be wrapped at word
boundaries (ie whitespace) if they are too long
to fit within the allocated width. If set to false,
the lines will be wrapped at character boundaries.
Determines the offset of the start of the given line.
Parameters:
line - the line number to translate >= 0
Returns:
the offset >= 0
Throws:
BadLocationException - thrown if the line is
less than zero or greater or equal to the number of
lines contained in the document (as reported by
getLineCount).
Determines the offset of the end of the given line.
Parameters:
line - the line >= 0
Returns:
the offset >= 0
Throws:
BadLocationException - Thrown if the line is
less than zero or greater or equal to the number of
lines contained in the document (as reported by
getLineCount).
public void replaceRange(String str,
int start,
int end)
Replaces text from the indicated start to end position with the
new text specified. Does nothing if the model is null. Simply
does a delete if the new string is null or empty.
This method is thread safe, although most Swing methods
are not. Please see
Threads
and Swing for more information.
Gets column width.
The meaning of what a column is can be considered a fairly weak
notion for some fonts. This method is used to define the width
of a column. By default this is defined to be the width of the
character m for the font used. This method can be
redefined to be some alternative amount.
Returns a string representation of this JTextArea. This method
is intended to be used only for debugging purposes, and the
content and format of the returned string may vary between
implementations. The returned string may be empty but may not
be null.
Returns true if a viewport should always force the width of this
Scrollable to match the width of the viewport. This is implemented
to return true if the line wrapping policy is true, and false
if lines are not being wrapped.
true if a viewport should force the Scrollables width
to match its own.
getPreferredScrollableViewportSize
public DimensiongetPreferredScrollableViewportSize()
Returns the preferred size of the viewport if this component
is embedded in a JScrollPane. This uses the desired column
and row settings if they have been set, otherwise the superclass
behavior is used.
public int getScrollableUnitIncrement(Rectangle visibleRect,
int orientation,
int direction)
Components that display logical rows or columns should compute
the scroll increment that will completely expose one new row
or column, depending on the value of orientation. This is implemented
to use the vaules returned by the getRowHeight and
getColumnWidth methods.
Scrolling containers, like JScrollPane, will use this method
each time the user requests a unit scroll.
Gets the AccessibleContext associated with this JTextArea.
For JTextAreas, the AccessibleContext takes the form of an
AccessibleJTextArea.
A new AccessibleJTextArea instance is created if necessary.
Submit a bug or feature For further API reference and developer documentation, see Java 2 SDK SE Developer Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Java, Java 2D, and JDBC are trademarks or registered trademarks of Sun Microsystems, Inc. in the US and other countries. Copyright 1993-2001 Sun Microsystems, Inc. 901 San Antonio Road Palo Alto, California, 94303, U.S.A. All Rights Reserved.