Java JTextArea

The object of a JTextArea class is a multi-line region that displays text. It allows the editing of multiple-line text. It inherits the JTextComponent class. An editable and showing multi-line text component in Java is represented by the JTextArea class, which is a component of the javax.swing package.

It is a flexible instrument for managing text in graphical user interfaces (GUIs). It provides extra features designed for multi-line text display and input while inheriting fundamental text editing and manipulation functionality from the JTextComponent class, from which it extends.

This class is frequently used in graphical user interface (GUI) applications where users must enter or view text that crosses numerous lines, like in text editors, chat programmes, or form fields. Developers may design dynamic, interactive interfaces for effective text input and manipulation with JTextArea.

JTextArea Class Declaration

Let's see the declaration for javax.swing.JTextArea class.

JTextArea is a subclass of JTextComponent that offers additional features specifically designed for handling multi-line text material in addition to text editing and rendering functionality. JTextArea is a tool that developers can use to build interactive graphical user interface (GUI) applications. These apps can be used to show enormous amounts of text, allow user input in text areas, or construct text editors that support multiple lines of text.

Commonly Used Constructors

ConstructorDescription
JTextArea()Creates a text area that displays no text initially.
JTextArea(String s)Creates a text area that displays specified text initially.
JTextArea(int row, int column)Creates a text area with the specified number of rows and columns that displays no text initially.
JTextArea(String s, int row, int column)Creates a text area with the specified number of rows and columns that displays specified text.

Commnly Used Methods

MethodsDescription
void setRows(int rows)It is used to set specified number of rows.
void setColumns(int cols)It is used to set specified number of columns.
void setFont(Font f)It is used to set the specified font.
void insert(String s, int position)It is used to insert the specified text on the specified position.
void append(String s)It is used to append the given text to the end of the document.
int getRows()It retrieves the current number of rows in the JTextArea.
int getColumns()It retrieves the current number of columns in the JTextArea.
Font getFont()It retrieves the current font used for text rendering in the JTextArea.
void setText(String text)It sets the text content of the JTextArea to the specified text.
String getText()It retrieves the text content of the JTextArea.
void setEditable(boolean editable)It sets whether the JTextArea is editable or not.
void setLineWrap(boolean wrap)It sets whether lines in the JTextArea should be wrapped if they are too long to fit within the width of the JTextArea.
void setWrapStyleWord(boolean wrapWord)It sets whether words in the JTextArea should be wrapped at the nearest whitespace character.
void append(String str)It appends the specified text to the end of the JTextArea's current text content.
void insert(String str, int pos)It inserts the specified text at the specified position within the JTextArea.
void replaceRange(String str, int start, int end)It replaces the text content within the specified range with the specified text.
void setForeground(Color fg)It sets the foreground (text) color of the JTextArea's text.
void setBackground(Color bg)It sets the background color of the JTextArea.

These techniques provide a range of functions for adjusting JTextArea components' appearance, behaviour, and content in Swing-based GUI applications. It allows developers to tailor JTextAreas to their own needs, be it changing the design, adding text content, or performing text manipulation functions.

Java JTextArea Example

File Name: TextAreaExample.java

Output:

JAVA Jtextarea 1

Explanation

A JTextArea called area and a JFrame named f are initialized by the code. The text "Welcome to javatpoint" is initialized in the JTextArea. The position and size of the JTextArea are set to (10,30) with a wide of 200 pixels and a height of 200 pixels using the setBounds() method. The JFrame is then updated with the JTextArea. The layout manager is specifically set to null, and the JFrame is configured with a size of 300x300 pixels. When the JFrame's visibility is set to true, it is finally visible on the screen.

Java JTextArea Example with ActionListener

File Name: TextAreaExample.java

Output:

JAVA Jtextarea 2

Explanation

The above Java program generates a Swing GUI application with a JTextArea for text input, a JButton to count the characters and words in the text input, and a JLabels display for the results.The constructor of the TextAreaExample class initialises the JFrame and the GUI elements. The word count and character count are displayed by two JLabels (l1 and l2), respectively.

To begin the counting process, a JButton (b) with the label "Count Words" is created and a JTextArea (area) is configured for text input. The TextAreaExample class itself is selected as the button's ActionListener. Every element is included in the JFrame (f).

To handle button clicks, the ActionListener interface requires the implementation of the actionPerformed function. getText() is used to retrieve the text entered into the JTextArea upon clicking of the button. The split() method is then used to divide the text into words based on whitespace, utilising the regular expression "\s" to do so.

The setText() function on the JLabels is used to count and show the number of words and characters.The Main method creates the TextAreaExample object and initialises and displays the GUI for the user.

Conclusion

To sum up, the Java JTextArea class is an essential tool for handling multi-line text in Swing-based graphical user interfaces (GUIs). It provides a multitude of functions for text display, editing, and manipulation by virtue of its inheritance from JTextComponent. Developers can design dynamic and interactive GUI applications, such text editors, chat programmes, or form fields, where users must input or see text across several lines, because to its flexibility and variety.All things considered, JTextArea is a potent part of Java's Swing toolkit that lets programmers design intricate and intuitive text-based user interfaces for a range of applications. Through efficient utilisation of its features, developers can augment the user experience and craft solid graphical user interface programmes customised to meet certain demands and specifications.