Add ActionListener to JButton in Java

Rupam Yadav Feb 06, 2022 Dec 29, 2021
  1. Add ActionListener to a JButton Using an Anonymous Class
  2. Add ActionListener to JButton Using Anonymous Class and Lambda
  3. Add ActionListener to JButton Using Inheritance
Add ActionListener to JButton in Java

Today we will discuss the ActionListener interface of the java.awt.event and how to add it to a JButton, a component of the JFrame class in the Java Swing package.

Add ActionListener to a JButton Using an Anonymous Class

In the first example, we create a class JavaExample with a method main(), and in that method, we create a JFrame object. In the JFrame window, we create three components: a JLabel to show a message, a JTextField to take an input, and a JButton to which we will add the ActionListener.

To add the listener, we call the addActionListener() function of the JButton class, and in this method, we create an anonymous class and override the actionPerformed() function that is a part of the ActionListener interface. actionPerformed() is a method that is called when an action is performed.

In the actionPerformed() method, we first check if there is anything in the JTextField, and if the Text Field is empty, then we show a message using JLabel. Otherwise, we display the message that we write in the Text Field.

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class JavaExample {
    public static void main(String[] args) {

        JFrame jFrame = new JFrame("Java Example");

        JLabel jLabel = new JLabel();
        jLabel.setBounds(50, 150, 350, 40);

        final JTextField jTextField = new JTextField();
        jTextField.setBounds(50, 50, 150, 20);

        JButton jButton = new JButton("Submit");
        jButton.setBounds(50, 100, 100, 30);

        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (!jTextField.getText().equals(""))
                    jLabel.setText(jTextField.getText());
                else
                    jLabel.setText("Please write something in the edit box");
            }
        });
        jFrame.add(jLabel);
        jFrame.add(jButton);
        jFrame.add(jTextField);
        jFrame.setSize(400, 400);
        jFrame.setLayout(null);
        jFrame.setVisible(true);
    }
}

Output:

add actionlistener to jbutton 1

Add ActionListener to JButton Using Anonymous Class and Lambda

In Java 8, Lambda Expressions were added that we would use in this example. Everything is the same as the previous example, but in the program, we create a method buttonPressed(), and in that function, we print the message written in the text field to a JLabel.

To add the ActionListener to the JButton, we use the addActionListener() function, and in that method, we use the lambda approach. We use the parameter e that is an ActionEvent object, then call the buttonPressed() method.

import javax.swing.*;

public class JavaExample {
    static JTextField jTextField;
    static JLabel jLabel;

    public static void main(String[] args) {

        JFrame jFrame = new JFrame("Java Example");

        jLabel = new JLabel();
        jLabel.setBounds(50, 150, 350, 40);

        jTextField = new JTextField();
        jTextField.setBounds(50, 50, 150, 20);

        JButton jButton = new JButton("Submit");
        jButton.setBounds(50, 100, 100, 30);

        jButton.addActionListener(e -> buttonPressed());
        
        jFrame.add(jLabel);
        jFrame.add(jButton);
        jFrame.add(jTextField);
        
        jFrame.setSize(400, 400);
        jFrame.setLayout(null);
        jFrame.setVisible(true);
    }

    static void buttonPressed() {
        if (!jTextField.getText().equals("")) {
            jLabel.setText(jTextField.getText());
        } else {
            jLabel.setText("Please write something in the edit box");
        }
    }
}

Output:

add actionlistener to jbutton 2

Add ActionListener to JButton Using Inheritance

In this program, we add the ActionListener using the concept of inheritance.

We create a class JavaExample and then extend the class JFrame and implement the ActionListener interface. We have to override the actionPerformed() method that is a part of the ActionListener interface.

We declare the JFrame component variables outside the main() method because we need to use it outside the function. We create a constructor of the JavaExample class and set all the components in it.

To add the ActionListener to the JButton component, we call the addActionListener() method and pass this, which points to the context of the current class as the current class JavaExample implements ActionListener it is a valid context to pass in the function.

In the actionPerformed() function, we write the operations that we want to perform when the button is pressed. In the main() method, we create an instance of JFrame and set its layout and visibility.

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class JavaExample extends JFrame implements ActionListener {
    static JTextField jTextField;
    static JLabel jLabel;
    static JButton jButton;

    public static void main(String[] args) {
        JFrame jFrame = new JavaExample();
        jFrame.setLayout(null);
        jFrame.setVisible(true);
    }

    public JavaExample() {
        jLabel = new JLabel();
        jLabel.setBounds(50, 150, 350, 40);

        jTextField = new JTextField();
        jTextField.setBounds(50, 50, 150, 20);

        jButton = new JButton("Submit");
        jButton.setBounds(50, 100, 100, 30);

        jButton.addActionListener(this);

        add(jLabel);
        add(jButton);
        add(jTextField);

        setSize(400, 400);

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (!jTextField.getText().equals("")) {
            jLabel.setText(jTextField.getText());
        } else {
            jLabel.setText("Please write something in the edit box");
        }
    }
}

Output:

add actionlistener to jbutton 3

Author: Rupam Yadav
Rupam Yadav avatar Rupam Yadav avatar

Rupam Saini is an android developer, who also works sometimes as a web developer., He likes to read books and write about various things.

LinkedIn

Related Article - Java Swing

Related Article - Java JLabel