May 24, 2011, 8:50 p.m.
posted by pythonrocks
How to Use Text Areas
The JTextArea class provides a component that displays multiple lines of text, optionally allowing the user to edit the text. If you need to obtain only one line of input from the user, you should use a text field instead. If you want the text area to display its text using multiple fonts or other styles, you should use an editor pane or text pane instead. If the displayed text has a limited length and is never edited by the user, consider using a label instead.
Many of this book's examples use uneditable text areas to display program output. Figure is a picture of one that lets you enter text using a text field (at the top) and then appends the entered text to a text area (underneath).
textArea = new JTextArea(5, 20); JScrollPane scrollPane = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); textArea.setEditable(false);
The two arguments to the JTextArea constructor are hints as to the number of rows and columns, respectively, the text area should display. The scroll pane that contains the text area pays attention to these hints when determining how big the scroll pane should be.
Without the creation of the scroll pane, the text area would not automatically scroll. The JScrollPane constructor shown in the preceding snippet sets up the text area for viewing in a scroll pane and specifies that the scroll pane's scroll bars should both always be visible. See How to Use Scroll Panes (page 325) if you want further information.
By default, text areas are editable. The code setEditable(false) makes the text area uneditable. It is still selectable and the user can copy data from it, but the user can't change the text area's contents directly.
The following code adds text to the text area. Note that the text system uses the \n character internally to represent newlines; for details, see the API documentation for Default- EditorKit.
private final static String newline = "\n"; ... textArea.append(text + newline);
Unless the user has moved the caret (insertion point) by clicking or dragging in the text area, the text area automatically scrolls so that the appended text is visible. You can force the text area to scroll to the bottom by moving the caret to the end of the text area, like this, after the call to append:
Customizing Text Areas
You can customize text areas in several ways. For example, although a given text area can display text in only one font and color, you can (as for any component) set which font and color it uses. You can also determine how the text area wraps lines and the number of characters per tab. Finally, you can use the methods JTextArea inherits from JTextComponent to set properties such as the caret, support for dragging, selection color, and so on.
The following code, taken from TextSamplerDemo.java, demonstrates initializing an editable text area. The text area uses the specified italic font and wraps lines between words.
JTextArea textArea = new JTextArea( "This is an editable JTextArea. " + "A text area is a \"plain\" text component, " + "which means that although it can display text " + "in any font, all of the text is in the same font." ); textArea.setFont(new Font("Serif", Font.ITALIC, 16)); textArea.setLineWrap(true); textArea.setWrapStyleWord(true);
By default, a text area doesn't wrap lines that are too long for the display area. Instead it uses one line for all the text between newline characters and—if the text area is within a scroll pane—allows itself to be scrolled horizontally. This example turns line wrapping on with a call to setLineWrap and then calls setWrapStyleWord to indicate that the text area should wrap lines at word boundaries rather than at character boundaries.
To provide scrolling capability, the example puts the text area in a scroll pane.
JScrollPane areaScrollPane = new JScrollPane(textArea); areaScrollPane.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); areaScrollPane.setPreferredSize(new Dimension(250, 250));
You might have noticed that the JTextArea constructor used in this example does not specify the number of rows or columns. Instead, the code limits the size of the text area by setting the scroll pane's preferred size.
The Text Area API
Figure through 95 list the commonly used JTextArea constructors and methods. Other methods you are likely to call are defined in JTextComponent and listed in The Text Component API (page 77). You should also refer to the JTextArea API documentation at: http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JTextArea.html.
You might also invoke methods on a text area that it inherits from its other ancestors, such as setPreferredSize, setForeground, setBackground, setFont, and so on. See The JComponent Class (page 53) for tables of commonly used inherited methods.
Examples That Use Text Areas
Many of this book's examples use JTextArea, typically to provide an area where events are logged. Here's a partial list of the demos that use text areas.