CIS 35A: Introduction to Java Programming

Home | Green Sheet | Lectures | Assignments | FAQ | Grades

Layout

Controls and layout managers
Components
Text areas

A frame with a text area

Common constructors of the JTextArea class

Constructor Description
JTextArea(intRows, intCols) Creates an empty text area with the specified number of rows and columns.
JTextArea(String, intRows, intCols) Creates a text area with the specified number of rows and columns starting with the specified text.

Some methods that work with text areas

Method Description
setLineWrap(boolean) If the boolean value is true, the lines will wrap if they don't fit.
setWrapStyleWord(boolean) If the boolean value is true and line wrapping is turned on, wrapped lines will be separated between words.
append(String) Appends the specified string to the text in the text area.
getText() Returns the text in the text area as a String.
setText(String) Sets the text in the text area to the specified string.

Code that creates a text area

private JTextArea commentTextArea;
commentTextArea = new JTextArea(7, 20);
commentTextArea.setLineWrap(true);
commentTextArea.setWrapStyleWord(true);
add(commentTextArea);

Code that gets the text stored in the text area

String comments = commentTextArea.getText();

Note

  • If the text area is going to receive more text than can be viewed at one time, you should add the text area to a scroll pane.
Previous | Summary | Text areas | Scroll panes | Check boxes | Radio buttons | Borders | Combo boxes | Event listeners | Lists | Multiple selections in a list | List models | Next