public class JTextArea extends javax.swing.text.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
DocumentEvents delivered to registered
DocumentListeners, 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.
TextArea,
JTextComponent,
JTextField,
JTextPane,
JEditorPane,
Document,
DocumentEvent,
DocumentListener,
Serialized Form| Modifier and Type | Class and Description |
|---|---|
protected class |
JTextArea.AccessibleJTextArea
Provides accessibility support for
JTextArea. |
javax.swing.text.JTextComponent.AccessibleJTextComponent, javax.swing.text.JTextComponent.KeyBindingJComponent.AccessibleJComponentaccessibleContext, listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW| Constructor and Description |
|---|
JTextArea()
Creates a new
JTextArea object. |
JTextArea(javax.swing.text.Document doc)
Creates a new
JTextArea object. |
JTextArea(javax.swing.text.Document doc,
java.lang.String text,
int rows,
int columns)
Creates a new
JTextArea object. |
JTextArea(int rows,
int columns)
Creates a new
JTextArea object. |
JTextArea(java.lang.String text)
Creates a new
JTextArea object. |
JTextArea(java.lang.String text,
int rows,
int columns)
Creates a new
JTextArea object. |
| Modifier and Type | Method and Description |
|---|---|
void |
append(java.lang.String toAppend)
Appends the supplied text to the current contents
of the document model.
|
protected javax.swing.text.Document |
createDefaultModel()
Creates the default document model.
|
javax.accessibility.AccessibleContext |
getAccessibleContext()
Returns the accessible context associated with the
JTextArea. |
int |
getColumns()
Returns the current number of columns.
|
protected int |
getColumnWidth() |
int |
getLineCount() |
int |
getLineEndOffset(int line) |
int |
getLineOfOffset(int offset) |
int |
getLineStartOffset(int line) |
boolean |
getLineWrap()
Checks whether line wrapping is enabled.
|
java.awt.Dimension |
getPreferredScrollableViewportSize()
Returns the preferred size of that text component in the case
it is embedded within a JScrollPane.
|
java.awt.Dimension |
getPreferredSize()
Returns the preferred size for the JTextArea.
|
protected int |
getRowHeight() |
int |
getRows()
Returns the current number of rows.
|
boolean |
getScrollableTracksViewportWidth()
Returns true if the width of this component should be forced
to match the width of a surrounding view port.
|
int |
getScrollableUnitIncrement(java.awt.Rectangle visibleRect,
int orientation,
int direction)
Returns the increment that is needed to expose exactly one new line
of text.
|
int |
getTabSize()
Returns the number of characters used for a tab.
|
java.lang.String |
getUIClassID()
Returns the UI class ID string.
|
boolean |
getWrapStyleWord()
Checks whether word style wrapping is enabled.
|
void |
insert(java.lang.String string,
int position)
Inserts the supplied text at the specified position.
|
void |
replaceRange(java.lang.String text,
int start,
int end) |
void |
setColumns(int columns)
Sets the number of rows.
|
void |
setLineWrap(boolean flag)
Enables/disables line wrapping.
|
void |
setRows(int rows)
Sets the number of rows.
|
void |
setTabSize(int newSize)
Sets the number of characters used for a tab to the
supplied value.
|
void |
setWrapStyleWord(boolean flag)
Enables/Disables word style wrapping.
|
addCaretListener, addInputMethodListener, addKeymap, copy, cut, fireCaretUpdate, getActions, getCaret, getCaretColor, getCaretListeners, getCaretPosition, getDisabledTextColor, getDocument, getDragEnabled, getFocusAccelerator, getHighlighter, getInputMethodListeners, getKeymap, getKeymap, getMargin, getNavigationFilter, getScrollableBlockIncrement, getScrollableTracksViewportHeight, getSelectedText, getSelectedTextColor, getSelectionColor, getSelectionEnd, getSelectionStart, getText, getText, getToolTipText, getUI, isEditable, loadKeymap, modelToView, moveCaretPosition, paramString, paste, read, removeCaretListener, removeInputMethodListener, removeKeymap, replaceSelection, select, selectAll, setCaret, setCaretColor, setCaretPosition, setDisabledTextColor, setDocument, setDragEnabled, setEditable, setFocusAccelerator, setHighlighter, setKeymap, setMargin, setNavigationFilter, setSelectedTextColor, setSelectionColor, setSelectionEnd, setSelectionStart, setText, setUI, updateUI, viewToModel, writeaddAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setNextFocusableComponent, setOpaque, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, updateadd, add, add, add, add, addContainerListener, addImpl, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalKeys, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusDownCycle, validate, validateTreeaction, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setMaximumSize, setMinimumSize, setName, setPreferredSize, setSize, setSize, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCyclepublic JTextArea()
JTextArea object.public JTextArea(java.lang.String text)
JTextArea object.text - the initial textpublic JTextArea(int rows, int columns)
JTextArea object.rows - the number of rowscolumns - the number of colsjava.lang.IllegalArgumentException - if rows or columns are negativepublic JTextArea(java.lang.String text, int rows, int columns)
JTextArea object.text - the initial textrows - the number of rowscolumns - the number of colsjava.lang.IllegalArgumentException - if rows or columns are negativepublic JTextArea(javax.swing.text.Document doc)
JTextArea object.doc - the document model to usepublic JTextArea(javax.swing.text.Document doc, java.lang.String text, int rows, int columns)
JTextArea object.doc - the document model to usetext - the initial textrows - the number of rowscolumns - the number of colsjava.lang.IllegalArgumentException - if rows or columns are negativepublic void append(java.lang.String toAppend)
toAppend - the text to appendprotected javax.swing.text.Document createDefaultModel()
public boolean getScrollableTracksViewportWidth()
getScrollableTracksViewportWidth in interface ScrollablegetScrollableTracksViewportWidth in class javax.swing.text.JTextComponentpublic int getScrollableUnitIncrement(java.awt.Rectangle visibleRect, int orientation, int direction)
getRowHeight() and getColumnWidth(), depending on
the value of the argument direction.getScrollableUnitIncrement in interface ScrollablegetScrollableUnitIncrement in class javax.swing.text.JTextComponentvisibleRect - the view area that is visible in the viewportorientation - either SwingConstants.VERTICAL or
SwingConstants.HORIZONTALdirection - less than zero for up/left scrolling, greater
than zero for down/right scrollingjava.lang.IllegalArgumentException - if orientation is invalidpublic java.awt.Dimension getPreferredScrollableViewportSize()
getPreferredScrollableViewportSize in interface ScrollablegetPreferredScrollableViewportSize in class javax.swing.text.JTextComponentpublic java.lang.String getUIClassID()
getUIClassID in class javax.swing.text.JTextComponentJComponent.setUI(javax.swing.plaf.ComponentUI),
JComponent.updateUI()public int getColumns()
public void setColumns(int columns)
columns - number of columnsjava.lang.IllegalArgumentException - if columns is negativepublic int getRows()
public void setRows(int rows)
rows - number of rowsjava.lang.IllegalArgumentException - if rows is negativepublic boolean getLineWrap()
true if line wrapping is enabled,
false otherwisepublic void setLineWrap(boolean flag)
flag - true to enable line wrapping,
false otherwisepublic boolean getWrapStyleWord()
true if word style wrapping is enabled,
false otherwisepublic void setWrapStyleWord(boolean flag)
flag - true to enable word style wrapping,
false otherwisepublic int getTabSize()
public void setTabSize(int newSize)
newSize - The new number of characters to use for a tab.protected int getColumnWidth()
public int getLineCount()
public int getLineStartOffset(int line) throws javax.swing.text.BadLocationException
javax.swing.text.BadLocationExceptionpublic int getLineEndOffset(int line) throws javax.swing.text.BadLocationException
javax.swing.text.BadLocationExceptionpublic int getLineOfOffset(int offset) throws javax.swing.text.BadLocationException
javax.swing.text.BadLocationExceptionprotected int getRowHeight()
public void insert(java.lang.String string, int position)
string - The string of text to insert.position - The position at which to insert the supplied text.java.lang.IllegalArgumentException - if the position is < 0 or greater
than the length of the current text.public void replaceRange(java.lang.String text, int start, int end)
public java.awt.Dimension getPreferredSize()
getColumns() and getRows().getPreferredSize in class JComponentComponent.setPreferredSize(java.awt.Dimension),
Component.getPreferredSize(),
Component.isPreferredSizeSet(),
ComponentUI.getPreferredSize(JComponent)public javax.accessibility.AccessibleContext getAccessibleContext()
JTextArea.getAccessibleContext in interface javax.accessibility.AccessiblegetAccessibleContext in class javax.swing.text.JTextComponentJTextArea