Event Handling in java Using Action listeners is probably very easy to implement, but the problem arises when you are having many actionlistener in one java class file and want to separate the set GUI part (primarily used to set the frame and other objects) from the ActionListener in your class file but it is way simpler than you think lets first see the steps to implement Actionlistener to the java class. ActionListener is a part of ‘java.util.event’ package it have only one method actionPerformed(ActionEvent) called when the user just performed the action.
Procedure: The steps to implement ActionListener are as follows:
- Step 1: Create an event handler class and specify that the class either implements an ActionListener interface or extends a class that implements an ActionListener interface.
- Step 2: Register an instance of the event handler class as a listener on one or more components.
- Step 3: Include code that implements the methods in listener interface.
- Step 4: Now create the file with the method of ActionListener known as actionPerformed(ActionEvent o).
Implementation: Let’s create the first java file with all the frames and other components.
Step 1:
public class ActionListenerClass implements ActionListener {
Step 2:
someComponent.addActionListener(instanceOfactionlsitenerclass); button.addActionListener(instanceOfactionlistenerclass) // It could be a button or any other component
Step 3:
For example:
public void actionPerformed(ActionEvent e) {
...// code that reacts to the action...
// The single argument to the method is an ActionEvent object
// that gives information about the event
}
Example 1-A:
Java
// Java Program demonstrating the use of// action listener from another java fileÂ
// Importing Swing classimport java.awt.event.ActionListener;// Importing ActionListener from awt packageimport javax.swing.*;import javax.swing.JButton;import javax.swing.JFrame;Â
// Class for actionlistenerpublic class GFGÂ
// Creating a new text field{Â Â Â Â static JTextField textfield = new JTextField();Â
    // Main driver method    public static void main(String[] args)    {        // Creating a button with no text and caption        JFrame frame = new JFrame();Â
        // Creating a button with the specified iccon object        JButton button = new JButton("button!");Â
        // Setting frame size using setSize(),setLayout(),        // setBoundsÂ
        // Setting width and height of a frame        // using setSize() method        frame.setSize(375, 250);Â
        frame.setLayout(null);        button.setBounds(40, 50, 100, 20);        textfield.setBounds(40, 20, 150, 20);Â
        // Component added to frame        frame.add(button);        frame.add(textfield);Â
        // Object of type actionlistener is created        // with reference of actionperformclass        ActionListener listener = new actionperformclass();Â
        // Adding the instance of event handler        // as listener of component        button.addActionListener(listener);        frame.setDefaultCloseOperation(            JFrame.EXIT_ON_CLOSE);        frame.setVisible(true);    }} |
Â
Â
Step 4: Now create the file with the actionPerformed(ActionEvent o) function of ActionListener.
Â
Example 1(B)
Â
Java
// Java Program that creates the file with the method// actionPerformed(ActionEvent o) of ActionListenerÂ
// Importing awt module and Swing classimport java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.*;Â
// Class// An action listener that prints a messagepublic class actionperformclass    extends classactionlistener implements ActionListener {Â
    // Method    public void actionPerformed(ActionEvent event)    {        // settext of textfield object of Jtextfield        textfield.setText("button is clicked");    }} |
Â
Â
Output: The above two programs will be generated by the class actionListener it is the driver class
Â
The flow is when the user clicks the Button, the button fires an action event which invokes the action listener’s actionPerformed method. Each time the user presses the button, the message is displayed in the text field.
Note: The class file of program should be present of both the java file.
Â

