JTextArea Class

Package: javax.swing

The JTextArea class creates a text area, which is similar to a text field, but it lets the user enter more than one line of text. If the user enters more text in the text area than can be displayed at once, the text area can display a scroll bar to allow the user to see the entire text. Figure 5-12 shows a text area in action.

9781118239742-fg0512.tif

Figure 5-12

CrossRef.eps To create the text area shown in Figure 5-12, you must use the JScrollPane class to create the scroll bars. For more information, see JScrollPane Class.

Constructors

Constructor

Description

JTextArea()

Creates a new text area

JTextArea(int rows, int cols)

Creates a new text area large enough to display the specified number of rows and columns

JTextArea(String text, int rows, int cols)

Creates a new text area with the specified initial text value, large enough to display the specified number of rows and columns

Methods

Method

Description

void append(String text)

Adds the specified text to the end of the text area’s text value.

int getLineCount()

Gets the number of lines currently in the text value.

String getText()

Gets the text value entered in the field.

void insert(String str, int pos)

Inserts the specified text at the specified position.

Get Java For Dummies Quick Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.