Using actionPerformed from Another Java Class
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
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.JButton;
import javax.swing.JFrame;
public class GFG
{
static JTextField textfield = new JTextField();
public static void main(String[] args)
{
JFrame frame = new JFrame();
JButton button = new JButton( "button!" );
frame.setSize( 375 , 250 );
frame.setLayout( null );
button.setBounds( 40 , 50 , 100 , 20 );
textfield.setBounds( 40 , 20 , 150 , 20 );
frame.add(button);
frame.add(textfield);
ActionListener listener = new actionperformclass();
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
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class actionperformclass
extends classactionlistener implements ActionListener {
public void actionPerformed(ActionEvent event)
{
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
Share your thoughts in the comments
Please Login to comment...