Ask Questions?

View Latest Questions

Advertisement


 
 

JTextArea
Posted on: July 26, 2006 at 12:00 AM
A javax.swing.JTextArea is a multi-line text component to display text or allow the user to enter text.

Java: JTextArea

Description

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.

Constructors

JTextArea ta = new JTextArea(int rows, int cols);
JTextArea ta = new JTextArea(String initialText, int rows, int cols);
 
 
 
 
 
 
 
 
 
 
 
 
 

Using a JTextArea for output -- Setting the text

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

ResultMethodDescription
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.

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

Scrolling

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);
content.add(scrollingResult);

Positioning the 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(2,2,2,2));

See Borders.

Example

See Example - JTextArea.

Other

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.

Copyleft 2005 Fred Swartz MIT License
Advertisement


DMCA.com