Java: JTextArea


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.

Using a JTextArea for output -- Setting the text

Assume: JTextArea ta; int i, w, pos, start, end, line; String s; boolean b; Font f;
Reader reader; Writer writer;

tanew JTextArea(rows, cols);Creates text area. cols is approx char width.
tanew JTextArea(s, rows, cols);As above, but also containing initial string s.
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.
Reading and writing to/from a JTextArea, null);Reads text from reader into text area. May throw IOException.
 ta.write(writer);Writes text from text area to writer. May throw IOException.


JTextArea doesn't support scrolling itself but you can easily add the JTextArea to a JScrollPane. JScrollPane creates scrollbars as needed. For example,

//... Create scrolling text area.
resultTA = new JTextArea("This is a test", 10, 80);
JScrollPane scrollingResult = new JScrollPane(resultTA);

Scrollbar policy

There are three policies that you can specify for the scrollbars. Below is how you set if for the horizontal scrollbar, but change "horizontal" to "vertical" as needed.

 scrollPane.setHorizontalScrollBarPolicy(policy);Where policy is described above.
 scrollPane.setVerticalScrollBarPolicy(policy);Where policy is described above.

It's Unncessary to have horizontal scrolling when wrapping is turned on. Wrapping takes precedence over horizontal scrolling.

Positioning the scrolling view to the top - moving the caret

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.

Space between text and edge improves appearance

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(4, 4, 4, 4));

See Borders.

Example: Common JTextArea+JScrollPane usage

Setting up a text area can be slightly tedious. Here is an example.

JTextArea outputTA = new JTextArea(12, 40);
. . .
outputTA.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));

JScrollPane scroller = new JScrollPane(outputTA);
. . .
content.add(scroller, ...);

Example: Getting and setting all lines sequentially

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");


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.