|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--java.awt.Component | +--java.awt.Container | +--javax.swing.JComponent | +--javax.swing.text.JTextComponent | +--javax.swing.JTextArea
Safe: A JTextArea
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 capabilities 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 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:
DocumentListener myListener = ??; JTextArea myArea = ??; myArea.getDocument().addDocumentListener(myListener);
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. As of 1.4, support for long term storage
of all JavaBeansTM
has been added to the java.beans
package.
Please see java.beans.XMLEncoder
.
JTextPane
,
JEditorPane
,
Serialized FormField Summary | |
private int |
columns
|
private int |
columnWidth
|
private int |
rowHeight
|
private int |
rows
|
private static String |
uiClassID
|
private boolean |
word
|
private boolean |
wrap
|
Fields inherited from class javax.swing.text.JTextComponent |
DEFAULT_KEYMAP, FOCUS_ACCELERATOR_KEY |
Fields inherited from class javax.swing.JComponent |
_bounds, accessibleContext, listenerList, paintingChild, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW |
Fields inherited from class java.awt.Container |
|
Fields inherited from class java.awt.Component |
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT |
Constructor Summary | |
JTextArea()
Enabled: Constructs a new TextArea. |
|
JTextArea(Document doc)
Enabled: 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)
Enabled: Constructs a new JTextArea with the specified number of rows and columns, and the given model. |
|
JTextArea(int rows,
int columns)
Enabled: Constructs a new empty TextArea with the specified number of rows and columns. |
|
JTextArea(String text)
Enabled: Constructs a new TextArea with the specified text displayed. |
|
JTextArea(String text,
int rows,
int columns)
Enabled: Constructs a new TextArea with the specified text and number of rows and columns. |
Method Summary | |
void |
append(String str)
Enabled: Appends the given text to the end of the document. |
protected Document |
createDefaultModel()
Creates the default implementation of the model to be used at construction if one isn't explicitly given. |
AccessibleContext |
getAccessibleContext()
Suppressed: Gets the AccessibleContext associated with this JTextArea. |
int |
getColumns()
Enabled: Returns the number of columns in the TextArea. |
protected int |
getColumnWidth()
Gets column width. |
int |
getLineCount()
Enabled: Determines the number of lines contained in the area. |
int |
getLineEndOffset(int line)
Enabled: Determines the offset of the end of the given line. |
int |
getLineOfOffset(int offset)
Enabled: Translates an offset into the components text to a line number. |
int |
getLineStartOffset(int line)
Enabled: Determines the offset of the start of the given line. |
boolean |
getLineWrap()
Enabled: Gets the line-wrapping policy of the text area. |
Dimension |
getPreferredScrollableViewportSize()
Enabled: Returns the preferred size of the viewport if this component is embedded in a JScrollPane. |
Dimension |
getPreferredSize()
Enabled: Returns the preferred size of the TextArea. |
protected int |
getRowHeight()
Defines the meaning of the height of a row. |
int |
getRows()
Enabled: Returns the number of rows in the TextArea. |
boolean |
getScrollableTracksViewportWidth()
Enabled: 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)
Enabled: 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()
Enabled: Gets the number of characters used to expand tabs. |
String |
getUIClassID()
Suppressed: Returns the class ID for the UI. |
boolean |
getWrapStyleWord()
Enabled: Gets the style of wrapping used if the text area is wrapping lines. |
void |
insert(String str,
int pos)
Enabled: Inserts the specified text at the specified position. |
protected String |
paramString()
Returns a string representation of this JTextArea. |
void |
replaceRange(String str,
int start,
int end)
Enabled: Replaces text from the indicated start to end position with the new text specified. |
void |
setColumns(int columns)
Enabled: Sets the number of columns for this TextArea. |
void |
setFont(Font f)
Enabled: Sets the current font. |
void |
setLineWrap(boolean wrap)
Enabled: Sets the line-wrapping policy of the text area. |
void |
setRows(int rows)
Enabled: Sets the number of rows for this TextArea. |
void |
setTabSize(int size)
Enabled: Sets the number of characters to expand tabs to. |
void |
setWrapStyleWord(boolean word)
Enabled: Sets the style of wrapping used if the text area is wrapping lines. |
private void |
writeObject(ObjectOutputStream s)
See readObject() and writeObject() in JComponent for more information about serialization in Swing. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
private static final String uiClassID
getUIClassID()
,
JTextComponent.readObject(java.io.ObjectInputStream)
private int rows
private int columns
private int columnWidth
private int rowHeight
private boolean wrap
private boolean word
Constructor Detail |
public JTextArea()
public JTextArea(String text)
text
- the text to be displayed, or nullpublic JTextArea(int rows, int columns)
rows
- the number of rows >= 0columns
- the number of columns >= 0public JTextArea(String text, int rows, int columns)
text
- the text to be displayed, or nullrows
- the number of rows >= 0columns
- the number of columns >= 0public JTextArea(Document doc)
doc
- the model to usepublic JTextArea(Document doc, String text, int rows, int columns)
doc
- the model to use, or create a default one if nulltext
- the text to be displayed, null if nonerows
- the number of rows >= 0columns
- the number of columns >= 0Method Detail |
public String getUIClassID()
getUIClassID
in class JComponent
JComponent.getUIClassID()
,
UIDefaults.getUI(javax.swing.JComponent)
protected Document createDefaultModel()
public void setTabSize(int size)
size
- number of characters to expand togetTabSize()
public int getTabSize()
public void setLineWrap(boolean wrap)
PropertyChange
event ("lineWrap") is fired when the policy is changed.
By default this property is false.
wrap
- indicates if lines should be wrappedgetLineWrap()
public boolean getLineWrap()
public void setWrapStyleWord(boolean word)
word
- indicates if word boundaries should be used
for line wrappinggetWrapStyleWord()
public boolean getWrapStyleWord()
setWrapStyleWord(boolean)
public int getLineOfOffset(int offset) throws BadLocationException
offset
- the offset >= 0
BadLocationException
public int getLineCount()
public int getLineStartOffset(int line) throws BadLocationException
line
- the line number to translate >= 0
BadLocationException
public int getLineEndOffset(int line) throws BadLocationException
line
- the line >= 0
BadLocationException
public void insert(String str, int pos)
This method is thread safe, although most Swing methods are not. Please see Threads and Swing for more information.
str
- the text to insertpos
- the position at which to insert >= 0TextComponent.setText(java.lang.String)
,
replaceRange(java.lang.String, int, int)
public void append(String str)
This method is thread safe, although most Swing methods are not. Please see Threads and Swing for more information.
str
- the text to insertinsert(java.lang.String, int)
public void replaceRange(String str, int start, int end)
This method is thread safe, although most Swing methods are not. Please see Threads and Swing for more information.
str
- the text to use as the replacementstart
- the start position >= 0end
- the end position >= startinsert(java.lang.String, int)
,
replaceRange(java.lang.String, int, int)
public int getRows()
public void setRows(int rows)
rows
- the number of rows >= 0getRows()
protected int getRowHeight()
public int getColumns()
public void setColumns(int columns)
columns
- the number of columns >= 0getColumns()
protected int getColumnWidth()
public Dimension getPreferredSize()
getPreferredSize
in class JComponent
JComponent.setPreferredSize(java.awt.Dimension)
,
ComponentUI
public void setFont(Font f)
setFont
in class JComponent
f
- the font to use as the current fontjava.awt.Component#getFont
protected String paramString()
null
.
paramString
in class JTextComponent
public boolean getScrollableTracksViewportWidth()
getScrollableTracksViewportWidth
in interface Scrollable
getScrollableTracksViewportWidth
in class JTextComponent
public Dimension getPreferredScrollableViewportSize()
getPreferredScrollableViewportSize
in interface Scrollable
getPreferredScrollableViewportSize
in class JTextComponent
JComponent.getPreferredSize()
public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction)
getRowHeight
and
getColumnWidth
methods.
Scrolling containers, like JScrollPane, will use this method each time the user requests a unit scroll.
getScrollableUnitIncrement
in interface Scrollable
getScrollableUnitIncrement
in class JTextComponent
visibleRect
- the view area visible within the viewportorientation
- Either SwingConstants.VERTICAL or
SwingConstants.HORIZONTAL.direction
- Less than zero to scroll up/left,
greater than zero for down/right.
JScrollBar.setUnitIncrement(int)
,
getRowHeight()
,
getColumnWidth()
private void writeObject(ObjectOutputStream s) throws IOException
s
- the ObjectOutputStream
in which to write
IOException
public AccessibleContext getAccessibleContext()
getAccessibleContext
in interface Accessible
getAccessibleContext
in class JTextComponent
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |