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.

void requestFocus()

Asks for the focus to be moved to this text field.

void replace Range(String str, int start, int end)

Replaces text indicated by the start and end positions with the new specified text.

void setColumns(int cols)

Sets the width of the text area. (It’s better to do this in the constructor.)

void setEditable (boolean value)

If false, makes the field read-only.

void setLineWrap (boolean value)

If true, wraps lines if the text doesn’t fit on one line.

void setText(String text)

Sets the field’s text value.

void setToolTipText (String text)

Sets the tooltip text that’s displayed if the user rests the mouse pointer over the text field for a few moments.

void setWrap StyleWord()

If true, wraps the text at word boundaries.

Creating a text area

Here’s the code I used to create the text area shown in Figure 5-12:

textNovel = new JTextArea(10, 25);

JScrollPane scroll = new JScrollPane(textNovel,

JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,

JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

The first statement creates a text area, giving it an initial size of 10 rows and 25 columns. Then the second statement creates a scroll pane. Notice that the text area object is passed as a parameter to the constructor for the JScrollPane, along with constants that indicate whether the scroll pane should include vertical or horizontal scroll bars (or both).

CrossRef.eps For more information about JScrollPane, see JScrollPane Class.

Retrieving text from a text area

To retrieve the text that the user enters in a text area, you use the getText method, like this:

String text = textNovel.getText();

You’ll usually write the code to retrieve text from a text area in the context of an action event listener. For example:

public void actionPerformed(ActionEvent e)

{

if (e.getSource() == buttonOK)

{

String text = textNovel.getText();

if (text.contains(“All work and no play”))

JOptionPane.showMessageDialog(textNovel,

“Can’t you see I’m working?”,

“Going Crazy”,

JOptionPane.ERROR_MESSAGE);

}

}

Here, a message box is displayed if the text contains the string All work and no play.

Notice that in addition to the getText method, the JTextArea class has methods that let you add text to the end of the text area’s current value (append), insert text into the middle of the value (insert), and replace text (replace). You use these methods to edit the value of the text area.

tip.eps Two of the JTextArea methods are used to control how lines longer than the width of the text area are handled. If you call setLineWrap with a value of true, lines that are too long to display are automatically wrapped to the next line; and if you call setWrapStyleWord with a value of true, any lines that are wrapped split between words instead of in the middle of a word. You usually use these two methods together, as follows:

textItinerary = new JTextArea(10, 20);

textItinerary.setLineWrap(true);

textItinerary.setWrapStyleWord(true);

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset