Java ActionListener Interface
The Java ActionListener is notified whenever you click on the button or menu item. It is notified against ActionEvent. The ActionListener interface is found in java.awt.event package. It has only one method: actionPerformed().
The actionPerformed() method is invoked automatically whenever you click on the registered component.
How to write ActionListener
The common approach is to implement the ActionListener. If you implement the ActionListener class, you need to follow 3 steps:
1) Implement the ActionListener interface in the class:
2) Register the component with the Listener:
3) Override the actionPerformed() method:
Java ActionListener Example: On Button click
Java ActionListener Example: Using Anonymous class
We can also use the anonymous class to implement the ActionListener. It is the shorthand way, so you do not need to follow the 3 steps:
Let us see the full code of ActionListener using anonymous class.