How to Use the JTextArea Class to Create Text Areas in Java - dummies

How to Use the JTextArea Class to Create Text Areas in Java

By Doug Lowe

Here, you will find a list of the most popular constructors and methods of the JTextArea class, which you use to create text areas in Java. In most cases, you use the second constructor, which lets you set the number of rows and columns to display. The rows parameter governs the height of the text area, and the cols parameter sets the width.

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.
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 replaceRange(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 (but 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 setWrapStyleWord() If true, wraps the text at word
boundaries.

To retrieve the text that the user enters in a text area, you use the getText method. Here’s a method from an action listener that retrieves text from a text area:

public void buttonOKClicked()
{
 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.

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