CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > Swing > JTextArea in Java Swing

Swing Tutorial

  • What Is Java Swing
  • First Program
  • What is AWT
  • Advantages of Swing Over AWT
  • JFrame
  • Swing Components
  • JLabel
  • JTextField
  • JPasswordField
  • JButton
  • Event Handling
  • FlowLayout
  • BorderLayout
  • GridLayout
  • GridBagLayout
  • BoxLayout
  • JPanel
  • JCheckBox
  • JRadioButton
  • Border Component
  • JComboBox
  • JList
  • JList with MVC
  • Mouse Event
  • Key Event
  • JMenuBar
  • JTextArea
  • Using Color
  • Use Font
  • Display Image/Icon
  • Dialog Box
  • JTable
  • JDesktopPane and JInternalFrame
  • JDesktopPane
  • JInternalFrame
  • Adapter Classes
  • Swing Timer
  • JScrollPane
  • JSlider
  • JProgressBar
  • JSeparator
  • JToolBar
  • ActionListener
  • ItemListener
  • Worker Thread
  • Nimbus Look and Feel

JTextArea in Java Swing

Learn the concepts, implementation details, and practical steps with a clean developer-focused walkthrough.

Yuba Raj Kalathoki
By Yuba Raj Kalathoki
Published: March 28, 2021 · 3 min read · 0 Comments
Share: X in 🔗

JTextArea is a component of the Java Swing library that allows users to input or display multiple lines of text. It is similar to the JTextField component, but it can handle multiple lines of text rather than just a single line.

Following is a basic example of how to use JTextArea in Java Swing:

import javax.swing.JFrame;
import javax.swing.JTextArea;

public class JTextAreaDemo extends JFrame {

    public JTextAreaDemo() {
        JTextArea textArea = new JTextArea();
        add(textArea);
        
        setTitle("JTextArea Demo");
        setSize(400, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

    public static void main(String[] args) {
        new JTextAreaDemo();
    }

}

Output:

JTextArea output 1
Added a Java code in JTextArea

In this example, a new JFrame is created with a JTextArea added to it. The JTextArea is then displayed on the frame with the setVisible(true) method.

JTextArea can also be customized by setting properties such as font, text color, and background color using the setFont(), setForeground(), and setBackground() methods, respectively. Additionally, we can use methods like setLineWrap() and setWrapStyleWord() to control how the text is wrapped within the component.

Customizing JTextArea

Following is an example of how to customize a JTextArea:

JTextArea textArea = new JTextArea();
textArea.setFont(new Font("Arial", Font.PLAIN, 14));
textArea.setForeground(Color.BLUE);
textArea.setBackground(Color.YELLOW);
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);

Let’s see how it looks after adding the customizable code:

import java.awt.Color;
import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JTextArea;

public class JTextAreaDemo extends JFrame {

    public JTextAreaDemo() {
        JTextArea textArea = new JTextArea();
        
        //Customization added to JTextArea
        textArea.setFont(new Font("Arial", Font.PLAIN, 14));
        textArea.setForeground(Color.BLUE);
        textArea.setBackground(Color.YELLOW);
        textArea.setLineWrap(true);
        textArea.setWrapStyleWord(true);

        
        add(textArea);
        
        setTitle("JTextArea Demo");
        setSize(400, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

    public static void main(String[] args) {
        new JTextAreaDemo();
    }

}

Output:

JTextArea customization output 2

In this example, the font is set to Arial with a plain style and a size of 14, the text color is set to blue, the background color is set to yellow, and line wrap and word wrap are enabled.

We can also make JTextArea scrollable. To add scroll ability to a JTextArea in Java Swing, we can wrap it inside a JScrollPane. This allows the user to scroll the text area if the content inside it exceeds the visible area.

Following is an example of how to add a JScrollPane to a JTextArea:

import java.awt.Color;
import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class JTextAreaDemo extends JFrame {

	public JTextAreaDemo() {
		JTextArea textArea = new JTextArea();
		// Adding scrollbar to jtext area
		JScrollPane scrollPane = new JScrollPane(textArea);
		add(scrollPane);

		// Customization added to JTextArea
		textArea.setFont(new Font("Arial", Font.PLAIN, 14));
		textArea.setForeground(Color.BLUE);
		textArea.setBackground(Color.YELLOW);
		textArea.setLineWrap(true);
		textArea.setWrapStyleWord(true);

		// Adding scrollPane to frame instead of jtextarea
		add(scrollPane);

		setTitle("JTextArea Demo");
		setSize(400, 400);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
	}

	public static void main(String[] args) {
		new JTextAreaDemo();
	}

}

In the example above, a new JScrollPane is created with the JTextArea inside it. The JScrollPane is then added to the JFrame instead of the JTextArea directly. When the content inside the JTextArea exceeds the visible area, a scrollbar will appear to allow the user to scroll through the text.

Output:

Scrollable JTextArea output

In the above output image, the content in JTextArea was more than the display window. Hence, there is a verticle scroll bar in the JTextArea. When the user wants to see all the content then s/he can scroll down to see the remaining content.

We can also customize the behavior of the scrollbar by using methods like setVerticalScrollBarPolicy() and setHorizontalScrollBarPolicy(). For example, to disable the vertical scrollbar, we can use:

scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);

This will remove the vertical scrollbar from the JTextArea, even if the content inside it exceeds the visible area. Similarly, we can use setHorizontalScrollBarPolicy() to control the horizontal scrollbar.

Related Posts:

  • Command Line Arguments in Java
  • What Is Java Swing? A Complete Guide to Java’s GUI Toolkit
  • Control Statements in Java
  • MySQL Commands for Developers
  • JTextField in Java Swing
  • Java 8+ String Enhancements: New Methods and Best Practices
Tags:javaswing
Was this article helpful?
← Previous ArticleDictionary Class in Java
Next Article →JTable in Java Swing

Recent Posts

  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • How to Fix SSH Agent Forwarding on macOS: The Ultimate Guide for Developers
  • How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)
  • How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error
  • Complete Guide to JaCoCo: How to Measure Java Code Coverage Accurately
CoderSathi

Your go-to resource for Java, Spring Boot, Microservices, AWS, and modern development tutorials.

Quick Links

  • About
  • Contact

Popular Topics

  • Java
  • Spring Boot
  • AWS
  • DevOps
  • MongoDB
  • Linux
  • Git
  • How to
© 2026 CoderSathi. All rights reserved. Privacy Policy · Sitemap