Prev Class | Next Class | Frames | No Frames |
Summary: Nested | Field | Method | Constr | Detail: Nested | Field | Method | Constr |
public class JTextArea
extends JTextComponent
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.
Nested Class Summary | |
protected class |
|
Nested classes/interfaces inherited from class javax.swing.text.JTextComponent | |
JTextComponent.AccessibleJTextComponent , JTextComponent.KeyBinding |
Nested classes/interfaces inherited from class javax.swing.JComponent | |
JComponent.AccessibleJComponent |
Nested classes/interfaces inherited from class java.awt.Container | |
Container.AccessibleAWTContainer |
Nested classes/interfaces inherited from class java.awt.Component | |
Component.AccessibleAWTComponent , Component.BltBufferStrategy , Component.FlipBufferStrategy |
Field Summary |
Fields inherited from class javax.swing.text.JTextComponent | |
DEFAULT_KEYMAP , FOCUS_ACCELERATOR_KEY |
Fields inherited from class javax.swing.JComponent | |
TOOL_TIP_TEXT_KEY , UNDEFINED_CONDITION , WHEN_ANCESTOR_OF_FOCUSED_COMPONENT , WHEN_FOCUSED , WHEN_IN_FOCUSED_WINDOW , accessibleContext , listenerList , ui |
Fields inherited from class java.awt.Component | |
BOTTOM_ALIGNMENT , CENTER_ALIGNMENT , LEFT_ALIGNMENT , RIGHT_ALIGNMENT , TOP_ALIGNMENT |
Fields inherited from interface java.awt.image.ImageObserver | |
ABORT , ALLBITS , ERROR , FRAMEBITS , HEIGHT , PROPERTIES , SOMEBITS , WIDTH |
Constructor Summary | |
| |
| |
Method Summary | |
void | |
protected Document |
|
AccessibleContext |
|
protected int | |
int |
|
int | |
int |
|
int |
|
int |
|
boolean |
|
Dimension |
|
Dimension |
|
protected int | |
int |
|
boolean |
|
int |
|
int |
|
String |
|
boolean |
|
void | |
void |
|
void |
|
void |
|
void |
|
void |
|
void |
|
Methods inherited from class java.lang.Object | |
clone , equals , extends Object> getClass , finalize , hashCode , notify , notifyAll , toString , wait , wait , wait |
public JTextArea(int rows, int columns)
Creates a newJTextArea
object.
- Parameters:
rows
- the number of rowscolumns
- the number of cols
- Throws:
IllegalArgumentException
- if rows or columns are negative
public JTextArea(String text)
Creates a newJTextArea
object.
- Parameters:
text
- the initial text
public JTextArea(String text, int rows, int columns)
Creates a newJTextArea
object.
- Parameters:
text
- the initial textrows
- the number of rowscolumns
- the number of cols
- Throws:
IllegalArgumentException
- if rows or columns are negative
public JTextArea(Document doc)
Creates a newJTextArea
object.
- Parameters:
doc
- the document model to use
public void append(String toAppend)
Appends the supplied text to the current contents of the document model.
- Parameters:
toAppend
- the text to append
protected Document createDefaultModel()
Creates the default document model.
- Returns:
- a new default model
public AccessibleContext getAccessibleContext()
Returns the accessible context associated with theJTextArea
.
- Specified by:
- getAccessibleContext in interface Accessible
- Overrides:
- getAccessibleContext in interface JTextComponent
- Returns:
- the accessible context associated with the
JTextArea
public boolean getLineWrap()
Checks whether line wrapping is enabled.
- Returns:
true
if line wrapping is enabled,false
otherwise
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.
- Specified by:
- getPreferredScrollableViewportSize in interface Scrollable
- Overrides:
- getPreferredScrollableViewportSize in interface JTextComponent
- Returns:
- the preferred size of that text component in the case it is embedded within a JScrollPane
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 pergetColumns()
andgetRows()
.
- Overrides:
- getPreferredSize in interface JComponent
- Returns:
- the preferred size of the JTextArea
public boolean getScrollableTracksViewportWidth()
Returns true if the width of this component should be forced to match the width of a surrounding view port. When line wrapping is turned on, this method returns true.
- Specified by:
- getScrollableTracksViewportWidth in interface Scrollable
- Overrides:
- getScrollableTracksViewportWidth in interface JTextComponent
- Returns:
- true if lines are wrapped.
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 ofgetRowHeight()
andgetColumnWidth()
, depending on the value of the argumentdirection
.
- Specified by:
- getScrollableUnitIncrement in interface Scrollable
- Overrides:
- getScrollableUnitIncrement in interface JTextComponent
- Parameters:
visibleRect
- the view area that is visible in the viewportorientation
- eitherSwingConstants.VERTICAL
orSwingConstants.HORIZONTAL
direction
- less than zero for up/left scrolling, greater than zero for down/right scrolling
- Returns:
- the increment that is needed to expose exactly one new row or column of text
- Throws:
IllegalArgumentException
- iforientation
is invalid
public int getTabSize()
Returns the number of characters used for a tab. This defaults to 8.
- Returns:
- the current number of spaces used for a tab.
public String getUIClassID()
Returns the UI class ID string.
- Overrides:
- getUIClassID in interface JTextComponent
- Returns:
- the string "TextAreaUI"
public boolean getWrapStyleWord()
Checks whether word style wrapping is enabled.
- Returns:
true
if word style wrapping is enabled,false
otherwise
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.
- Parameters:
string
- The string of text to insert.position
- The position at which to insert the supplied text.
- Throws:
IllegalArgumentException
- if the position is < 0 or greater than the length of the current text.
public void setColumns(int columns)
Sets the number of rows.
- Parameters:
columns
- number of columns
- Throws:
IllegalArgumentException
- if columns is negative
public void setLineWrap(boolean flag)
Enables/disables line wrapping.
- Parameters:
flag
-true
to enable line wrapping,false
otherwise
public void setRows(int rows)
Sets the number of rows.
- Parameters:
rows
- number of rows
- Throws:
IllegalArgumentException
- if rows is negative
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.
- Parameters:
newSize
- The new number of characters to use for a tab.
public void setWrapStyleWord(boolean flag)
Enables/Disables word style wrapping.
- Parameters:
flag
-true
to enable word style wrapping,false
otherwise