The
JTextArea
component provides a multi-line area for displaying
and editing plain text. The component is designed to act as a lightweight
replacement for the heavyweight
java.awt.TextArea
component,
which provides similar functionality using native widgets.
This component has additional functionality to the AWT class. It follows
the same design pattern as seen in other text components, such as
JTextField
,
JTextPane
and
JEditorPane
,
and embodied in
JTextComponent
. These classes separate the text
(the model) from its appearance within the onscreen component (the view). The
text is held within a
javax.swing.text.Document
object, which can
also maintain relevant style information where necessary. As a result, it is the
document that should be monitored for textual changes, via
DocumentEvent
s delivered to registered
DocumentListener
s, rather than this component.
Unlike
java.awt.TextArea
,
JTextArea
does not
handle scrolling. Instead, this functionality is delegated to a
JScrollPane
, which can contain the text area and handle
scrolling when required. Likewise, the word wrapping functionality
of the AWT component is converted to a property of this component
and the
rows
and
columns
properties
are used in calculating the preferred size of the scroll pane's
view port.
JTextArea
public JTextArea()
Creates a new JTextArea
object.
JTextArea
public JTextArea(int rows,
int columns)
Creates a new JTextArea
object.
rows
- the number of rowscolumns
- the number of cols
IllegalArgumentException
- if rows or columns are negative
JTextArea
public JTextArea(String text)
Creates a new JTextArea
object.
text
- the initial text
JTextArea
public JTextArea(String text,
int rows,
int columns)
Creates a new JTextArea
object.
text
- the initial textrows
- the number of rowscolumns
- the number of cols
IllegalArgumentException
- if rows or columns are negative
JTextArea
public JTextArea(Document doc,
String text,
int rows,
int columns)
Creates a new JTextArea
object.
text
- the initial textrows
- the number of rowscolumns
- the number of cols
IllegalArgumentException
- if rows or columns are negative
append
public void append(String toAppend)
Appends the supplied text to the current contents
of the document model.
toAppend
- the text to append
getColumns
public int getColumns()
Returns the current number of columns.
- number of columns
getLineWrap
public boolean getLineWrap()
Checks whether line wrapping is enabled.
true
if line wrapping is enabled,
false
otherwise
getPreferredScrollableViewportSize
public Dimension getPreferredScrollableViewportSize()
Returns the preferred size of that text component in the case
it is embedded within a JScrollPane. This uses the column and
row settings if they are explicitly set, or fall back to
the superclass's behaviour.
- getPreferredScrollableViewportSize in interface Scrollable
- getPreferredScrollableViewportSize in interface JTextComponent
- the preferred size of that text component in the case
it is embedded within a JScrollPane
getPreferredSize
public Dimension getPreferredSize()
Returns the preferred size for the JTextArea. This is the maximum of
the size that is needed to display the content and the requested size
as per
getColumns()
and
getRows()
.
- getPreferredSize in interface JComponent
- the preferred size of the JTextArea
getRows
public int getRows()
Returns the current number of rows.
- number of rows
getScrollableUnitIncrement
public int getScrollableUnitIncrement(Rectangle visibleRect,
int orientation,
int direction)
Returns the increment that is needed to expose exactly one new line
of text. This is implemented here to return the values of
getRowHeight()
and
getColumnWidth
, depending on
the value of the argument
direction
.
SwingConstants.HORIZONTAL
- getScrollableUnitIncrement in interface Scrollable
- getScrollableUnitIncrement in interface JTextComponent
visibleRect
- the view area that is visible in the viewportorientation
- either SwingConstants.VERTICAL
ordirection
- less than zero for up/left scrolling, greater
than zero for down/right scrolling
- the increment that is needed to expose exactly one new row
or column of text
IllegalArgumentException
- if orientation
is invalid
getTabSize
public int getTabSize()
Returns the number of characters used for a tab.
This defaults to 8.
- the current number of spaces used for a tab.
getWrapStyleWord
public boolean getWrapStyleWord()
Checks whether word style wrapping is enabled.
true
if word style wrapping is enabled,
false
otherwise
insert
public void insert(String string,
int position)
Inserts the supplied text at the specified position. Nothing
happens in the case that the model or the supplied string is null
or of zero length.
string
- The string of text to insert.position
- The position at which to insert the supplied text.
IllegalArgumentException
- if the position is < 0 or greater
than the length of the current text.
setLineWrap
public void setLineWrap(boolean flag)
Enables/disables line wrapping.
setTabSize
public void setTabSize(int newSize)
Sets the number of characters used for a tab to the
supplied value. If a change to the tab size property
occurs (i.e. newSize != tabSize), a property change event
is fired.
newSize
- The new number of characters to use for a tab.
setWrapStyleWord
public void setWrapStyleWord(boolean flag)
Enables/Disables word style wrapping.
flag
- true
to enable word style wrapping,
false
otherwise
JTextArea.java --
Copyright (C) 2004, 2005 Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version.