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:

  1. Multi-line text input:

JTextArea allows the user to input text in multiple lines.

  1. Word Wrapping:

In JTextArea you can set word wrapping to make the text automatically go to the next line.

  1. Scrolling:

Scrolling can be enabled using JScrollPane with JTextArea so that if the text is too long, the user can scroll to view it.

  1. Customizable size:

The number of rows and columns of JTextArea can be set, thereby controlling its size.

  1. Event Handling:

Event handling can be associated with JTextArea, such as performing an action when the text changes.

Constructors of JTextArea:

  1. JTextArea():

Creates an empty text area.

  1. JTextArea(String text):

This creates a JTextArea with text already set.

  1. JTextArea(int rows, int columns):

This creates a text area with a set number of rows and columns.

  1. 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:

  1. setText(String text):

Sets the text in a JTextArea.

  1. getText():

Gets the text from a JTextArea.

  1. setRows(int rows):

Sets the number of rows in a JTextArea.

  1. setColumns(int columns):

Sets the number of columns in a JTextArea.

  1. setLineWrap(boolean wrap):

Enables or disables line wrapping in a JTextArea. If set to true, the text automatically goes to a new line.

  1. 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.

  1. append(String text):

Appends new text to the end of the text already present in the JTextArea.

Benefits of JTextArea:

  1. Multi-line text input:

JTextArea is a suitable component for receiving multi-line text input from the user.

  1. Word wrapping:

JTextArea has options for word wrapping and line wrapping, allowing long text to be displayed systematically.

  1. Scrolling support:

Using JScrollPane with JTextArea you can add scrolling for long text.

  1. 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’

DS – Data structure Using C

OOP – Object Oriented Programming 

Java Programming

DBMS – Database Management System

RDBMS – Relational Database Management System

Join Now: Data Warehousing and Data Mining 

Leave a Reply

Your email address will not be published. Required fields are marked *