A javax.swing.JTextArea
is a multi-line text component
to display text or allow the user to enter text. For one line of text use
JTextField.
JTextArea ta = new JTextArea(int rows, int cols); JTextArea ta = new JTextArea(String initialText, int rows, int cols);
Assume: JTextArea ta; int i, w, pos, start, end, line; String s; boolean b; Font f;
Result | Method | Description |
---|---|---|
Setting text | ||
| ta.setText(s); | Replaces all text with s . |
| ta.append(s); | Appends s to the end. |
| ta.insert(s, pos); | Inserts s at position pos. |
| ta.replaceRange(s, start, end); | Replace start to end with s. |
Getting text | ||
s = | ta.getText(); | Returns all text. Use methods below to get individual lines. |
i = | ta.getLineCount(); | Returns number of lines in the text area. |
i = | ta.getLineStartOffset(line); | Returns character index of beginning of line line. May throw javax.swing.text.BadLocationException. |
i = | ta.getLineEndOffset(line); | Returns character index of end of line line. May throw javax.swing.text.BadLocationException. |
Changing the appearance/function | ||
| ta.setBorder(brdr); | Text is tight against edge. See example below to add space. |
| ta.setLineWrap(b); | Lines wrapped if true. Default false. |
| ta.setWrapStyleWord(b); | If wrapping on (see above), wraps at words (true) or chars (false). Default false. |
| ta.setTabSize(w); | Number of max width chars in a tab. |
| ta.setFont(f); | Displays using Font f. |
| ta.setEditable(b); | Set false to disable user editing. |
| ta.setCaretPosition(i); |
Set caret position. If content is scrolled, setCaretPosition(0) will move to top. |
The following code shows how to sequentially get and set each line
in a JTextArea
Assume inputArea
has the input and
outputArea
will receive the output.
Of course, this is a ridiculous way to copy from one text area to another;
it would be better to just write outputArea.setText(inputArea.getText());
,
but it wouldn't illustrate working with individual lines.
JTextArea inputArea = new JTextArea(40, 20); JTextArea outputArea = new JTextArea(40, 20); . . . outputArea.setText(""); // Empties the textarea String text = inputArea.getText(); int totalLines = inputArea.getLineCount(); for (int i=0; i < totalLines; i++) { int start = inputArea.getLineStartOffset(i); int end = inputArea.getLineEndOffset(i); String line = text.substring(start, end); outputArea.append(line + "\n"); }
JTextArea doesn't support scrolling itself but you can easily add the JTextArea to a JScrollPane. JScrollPane will create scrollbars as needed. For example,
//--- Create scrolling text area. resultTA = new JTextArea("This is a test", 10, 80); JScrollPane scrollingResult = new JScrollPane(resultTA); content.add(scrollingResult);
If more lines are added than can be displayed and the scrollbars appear, the position that shows can be controlled with setCaretPosition(pos), where pos is a character position between 0 and the length of the text. To move to the top, use position 0.
The caret-moving solution above to control what is displayed in a text area will change the position of the caret (text insertion point), which will change the selected area, etc. If you don't want the caret to change, you can specify that the displayed area must include character pos.
JTextArea resultTA = . . .
. . .
resultTA.scrollRectToVisible(result.modelToView(pos));
These methods work for all JTextComponent subclasses (eg, JTextField, JTextArea, JTextPane, and their subclasses).
JTextArea
leaves no space between the edge and
text that it holds. This can be fixed by adding an empty border
to it.
outArea.setBorder(BorderFactory.createEmptyBorder(2,2,2,2));
See Borders.
See Example - JTextArea.
JTextArea is a child class of JTextComponent, therefore the methods of JTextComponent are available, for example, to find and set the caret position or selected text, to handle cut, copy, paste operations to/from the clipboard, and even read and write methods.
If you use a text area only to display results (no user input), use ta.setEditable(false) to prevent the user from changing it.