Open In App

Using actionPerformed from Another Java Class

Improve
Improve
Like Article
Like
Save
Share
Report

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 class
import java.awt.event.ActionListener;
// Importing ActionListener from awt package
import javax.swing.*;
import javax.swing.JButton;
import javax.swing.JFrame;
 
// Class for actionlistener
public 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 class
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
 
// Class
// An action listener that prints a message
public 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.

 



Last Updated : 12 Jan, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads