It In this article JTextArea in Swing we give the information about JTextArea is used to receive multi-line text input from the user. It is used for long text, notes, or paragraph input where text in more than one line is required.
JTextArea in Swing:
JTextArea is an important component in Swing which is used to receive multi-line text input from the user. It is used for long text, notes, or paragraph input where text in more than one line is required.
Features of JTextArea:
- Multi-line text input:
JTextArea allows the user to input text in multiple lines.
- Word Wrapping:
In JTextArea you can set word wrapping to make the text automatically go to the next line.
- Scrolling:
Scrolling can be enabled using JScrollPane with JTextArea so that if the text is too long, the user can scroll to view it.
- Customizable size:
The number of rows and columns of JTextArea can be set, thereby controlling its size.
- Event Handling:
Event handling can be associated with JTextArea, such as performing an action when the text changes.
Constructors of JTextArea:
- JTextArea():
Creates an empty text area.
- JTextArea(String text):
This creates a JTextArea with text already set.
- JTextArea(int rows, int columns):
This creates a text area with a set number of rows and columns.
- JTextArea(String text, int rows, int columns):
It is creates a text area with a set initial text and a set number of rows and columns.
//Example of JTextArea
import javax.swing.*;
import java.awt.event.*;
public class JTextAreaExample {
public static void main(String[] args) {
// Create a JFrame (window)
JFrame frame = new JFrame(“JTextArea Example”);
frame.setSize(400, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create a JPanel to hold components
JPanel panel = new JPanel();
panel.setLayout(null); // No layout manager (manual placement)
frame.add(panel);
// Create a JTextArea
final JTextArea textArea = new JTextArea();
textArea.setBounds(10, 10, 360, 150); // Position and size
textArea.setLineWrap(true); // Enable line wrap
textArea.setWrapStyleWord(true); // Wrap whole words
panel.add(textArea);
// Create a JButton to trigger the action
JButton button = new JButton(“Show Text”);
button.setBounds(10, 170, 120, 25); // Position and size
panel.add(button);
// Add action listener to handle button click
button.addActionListener(new ActionListener() {
//@Override
public void actionPerformed(ActionEvent e) {
String userText = textArea.getText(); // Get text from JTextArea
System.out.println(“Text entered: ” + userText); // Output to console
JOptionPane.showMessageDialog(null, “You entered: ” + userText); // Show in dialog box
}
});
// Make the frame visible
frame.setVisible(true);
}
}
In this example:
- JTextArea has been used to create a multi-line text area with a capacity of 5 rows and 20 columns.
- Scrolling has been enabled via JScrollPane, allowing long text to be scrolled.
- Text is being obtained from JTextArea when a button is clicked.
Methods of JTextArea:
- setText(String text):
Sets the text in a JTextArea.
- getText():
Gets the text from a JTextArea.
- setRows(int rows):
Sets the number of rows in a JTextArea.
- setColumns(int columns):
Sets the number of columns in a JTextArea.
- setLineWrap(boolean wrap):
Enables or disables line wrapping in a JTextArea. If set to true, the text automatically goes to a new line.
- setWrapStyleWord(boolean word):
Enables word-wrapping in a JTextArea, so that the text automatically goes to the next line at the end of the line.
- append(String text):
Appends new text to the end of the text already present in the JTextArea.
Benefits of JTextArea:
- Multi-line text input:
JTextArea is a suitable component for receiving multi-line text input from the user.
- Word wrapping:
JTextArea has options for word wrapping and line wrapping, allowing long text to be displayed systematically.
- Scrolling support:
Using JScrollPane with JTextArea you can add scrolling for long text.
- Flexible and customizable:
Size of JTextArea can be controlled by setting number of rows and columns.
Summary:
- JTextArea is used for multi-line text input in Swing.
- It is used when there is a need to get long text or notes from the user.
- You can do word wrapping, scrolling and other customizations in JTextArea, making it a useful and flexible component for GUI applications.
Using JTextArea in Swing allows you to create complex text input applications, such as feedback forms or text editors.
Some More:
POP- Introduction to Programming Using ‘C’
OOP – Object Oriented Programming
DBMS – Database Management System
RDBMS – Relational Database Management System
Join Now: Data Warehousing and Data Mining