Java Program using JTextArea with ActionListener


In this program, You will learn how to implement JTextArea with ActionListener in java.


JTextArea jt;

public void actionPerformed(ActionEvent e) { 
     //statement
}

Example: How to implement JTextArea with ActionListener in java.

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;

class Test extends JFrame implements ActionListener {

    JTextArea jt;
    JButton jb1;

    Test() {

        jt = new JTextArea();
        jt.setBounds(50, 50, 150, 70);
        add(jt);

        jb1 = new JButton("Print");
        jb1.setBounds(50, 200, 100, 30);
        add(jb1);

        jb1.addActionListener(this);

        setLayout(null);
        setSize(600, 400);
        setVisible(true);

    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource().equals(jb1)) {
            JOptionPane.showMessageDialog(this, jt.getText());
        }
    }

    public static void main(String args[]) {
        Test t = new Test();
    }
}

Output:

JTextArea