How do I create a JTextArea?

This example shows you how to create a simple JTextArea component in Java Swing application. First you’ll create an instance of JTextAread and passes the number of rows and columns. Next to add a scroll ability you can wrap the component inside a JScrollPane.

package org.kodejava.example.swing;

import javax.swing.*;
import java.awt.*;

public class TextAreaDemo extends JPanel {
    public TextAreaDemo() {

    private void initializeUI() {
        this.setLayout(new BorderLayout());
        this.setPreferredSize(new Dimension(400, 200));

        // Creates a JTextArea with 5 rows and 40 colums. Then
        // wrap the JTextArea inside a JScrollPane for adding
        // scrolling abilities.
        JTextArea textArea = new JTextArea(5, 40);
        JScrollPane scrollPane = new JScrollPane(textArea);

        this.add(scrollPane, BorderLayout.CENTER);

    public static void showFrame() {
        JPanel panel = new TextAreaDemo();

        JFrame frame = new JFrame("JTextArea Demo");

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
Wayan Saryada

Wayan Saryada

A programmer, runner, recreational diver, currently living in the island of Bali, Indonesia. Mostly programming in Java, creating web based application with Spring Framework, JPA, etc. If you need help on Java programming you can hire me on Fiverr.
Wayan Saryada

Leave a Reply