To handle the user request effectively and efficiently the action listener interface of Java has to be implemented. Whenever an action has been performed in the application, the Java Action Listener receives a notification. It works according to the user’s actions. Whenever a user performs an action, the Action listener interface works and completes the action that has been asked by the user.
This article will discuss the implementation and working of ActionListener in Java.
How to Implement/Execute ActionListener in Java?
The action listener basically belongs to the “java.util.event” package. This works on the user’s actions. The action Performed method contains all the codes and the actions that have to be performed when an action occurs. For example, if a user clicks on the button corresponding to books available and presses the enter button further, this will invoke the Action performed method and it is sent to all the action Listeners corresponding to that relevant component.
Example: Creation of Button Using Action Listener
Let’s learn how to make a button using the action listener class in Java:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class actionexample {
public static void main(String[] args) {
// Create a frame
JFrame j = new JFrame("Action Listener Program");
//Create a button using the built-in method in Java
JButton a = new JButton("Click Here to Get Started");
//Set height, width, and axis respectively
a.setBounds(50, 100, 60, 30);
// Call the action listener class in Java
a.addActionListener(new ActionListener() {//The actionPerformed method that belongs to Actionlistener.
public void actionPerformed(ActionEvent d) {
}
});
//Set the size, visibility, and other Parameters
j.add(a);
j.setSize(400, 350);
j.setVisible(true);
j.setLayout(null);
}
}
In the above code,
- The relevant packages have been imported.
- A class has been declared for the ActionListener.
- The frames and the buttons have been declared accordingly.
- The height, width, and axis have been set according to the need.
- The ActionListener interface has been called for the action to be performed.
- The actionPerformed invokes the Action Listener on any action done by the user.
- Some parameters for the window have been set.
Output

In the above output:
- A complete window with the name of the Action Listener Program has been created.
- The text that has been inserted when clicked makes the actionPerformed method work.
- This ends the implementation of Action Listener in Java.
Conclusion
The Action Listener in Java works according to the user’s action. The actionPerfomed method invokes the parameters that were relevant to that specific component of Action Listener. In this article, we have documented the implementation of Action Listener in Java.