Swing JTextArea Class



Swing JTextArea Class :

package com.c4learn.swing;

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class JTextAreaDemo {

  private JFrame mainFrame;
  private JLabel headLabel;
  private JLabel msgLabel;
  private JPanel mainPanel;

  public JTextAreaDemo() {
    mainFrame = new JFrame("Java Swing Examples");
    mainFrame.setSize(400, 400);
    mainFrame.setLayout(new GridLayout(3, 1));

    headLabel = new JLabel("TextArea Demo", JLabel.CENTER);
    msgLabel = new JLabel("Blank Comment", JLabel.CENTER);
    mainPanel = new JPanel();
    mainPanel.setLayout(new FlowLayout());

    mainFrame.add(headLabel);
    mainFrame.add(mainPanel);
    mainFrame.add(msgLabel);
    mainFrame.setVisible(true);
  }

  public static void main(String[] args) {
    JTextAreaDemo swingControlDemo = new JTextAreaDemo();
    swingControlDemo.showListDemo();
  }

  private void showListDemo() {

    JLabel lblName = new JLabel("Comment :", JLabel.RIGHT);
    final JTextArea txtComment = new JTextArea(5, 20);

    JButton btnDisplay = new JButton("Display Comment");
    btnDisplay.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        String data = "Comment : " + txtComment.getText();
        msgLabel.setText(data);
      }
    });

    mainPanel.add(lblName);
    mainPanel.add(txtComment);
    mainPanel.add(btnDisplay);
    mainFrame.setVisible(true);
  }
}

Output :

Swing JTextArea Class