Updated March 20, 2023
Introduction to Java ActionListener
Java ActionListener, as the name says, is a type of class in the java programming language that receives a record whenever there is an action performed on the application. It is available along with the java.awt.event package and it is implemented with the help of the JButtons, and the syntax for the same is ‘Public interface ActionListener extends EventListener’. This class gets involved when there is any kind of action performed on the given space, like a click on a button, a click on a menu item, a click on any random space in the application, etc.
Declaration:
This interface can be declared using the following syntax.
Public interface ActionListener extends EventListener
Method:
There is only one method of ActionListener.
- actionPerformed(ActionEventev)
This method will get invoked when an action has occurred.
Steps to Create Java ActionListener
Normally, ActionListener works by implementing it. For that, the following steps can be performed.
Step 1: An event handler class had to be declared.
Step 2: While creating the class, mention whether the class is implementing or extending the ActionListener interface.
public class ActionExample Implements ActionListener
Step 3: Component or Event handler instance has to be registered as a Listener.
cmp.addActionListener(instanceOfActionListenerclass);
Step 4: Add the code that overrides the method actionPerformed() in the interface of ActionListener.
actionPerformed(ActionEvent evnt){ //specify the code that has to be implemented }
How Java ActionListener Works?
- When a user or person clicks a button, there should be an object in the program that implements the interface.
- As the object will be registered with an ActionListener, action will be triggered on clicking the button.
- This will invoke the method actionPerformed and performs the action mentioned in it.
Examples to Implement Java ActionListener
Below are the examples to implement java ActionListener:
Example #1
Code:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
//new class that implements the ActionListener interface
public class JActionListenerExample implements ActionListener {
//create a button
JButton btn;
//create a frame
JFrame frm;
//create a text area
JTextArea txt;
public JActionListenerExample() {
btn = new JButton("Click this button");
frm = new JFrame("Java ActionListener Example");
txt = new JTextArea(6, 41);
btn.addActionListener(this);
// line wrapping of text area
txt.setLineWrap(true);
//sets layout
frm.setLayout(new BorderLayout());
//add textarea to the container
frm.add(txt, BorderLayout.NORTH);
//add the button to the container
frm.add(btn, BorderLayout.SOUTH);
//Resize the window based on container size
frm.pack();
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//set visibility
frm.setVisible(true);
}
//action to be performed on clicking the button
@Override
public void actionPerformed(ActionEvent ev) {
//set text
txt.setText(txt.getText().concat("User has clicked the button \n"));
}
public static void main(String args[]) {
//create object
JActionListenerExample jl = new JActionListenerExample();
} }
Output:
An output as shown below will be displayed on executing the code.
Moreover, on clicking the button, a text will be displayed on the screen as depicted below.
Example #2
Code:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class JActionListenerExample {
//create a frame
private Frame fr;
//create a label 1
private Label lbl1;
//create a label 2
private Label lbl2;
//create a panel
private Panel pnl;
public JActionListenerExample(){
//call the method userint()
userint();
}
public static void main(String[] args){
//create the object of class
JActionListenerExample jl = new JActionListenerExample();
jl.ActnListener();
}
//definition of method userint
private void userint(){
//frame
fr = new Frame(" ActionListener Example");
//set the size of frame
fr.setSize(300,300);
//set the layout of frame
fr.setLayout(new GridLayout(4, 2));
//add the window listener
fr.addWindowListener(new WindowAdapter() {
//call when window is closing
public void windowClosing(WindowEvent wev){
System.exit(0);
}
});
//create label 1
lbl1 = new Label();
//set the alignment
lbl1.setAlignment(Label.CENTER);
//create label 2
lbl2 = new Label();
//set the alignment
lbl2.setAlignment(Label.CENTER);
//set the size
lbl2.setSize(450,200);
//create panel
pnl = new Panel();
//set the layout of panel
pnl.setLayout(new FlowLayout());
//add the label 1 to frame
fr.add(lbl1);
//add the panel to frame
fr.add(pnl);
//add the label 2 to frame
fr.add(lbl2);
//se the visibility as true
fr.setVisible(true);
}
//define the method
private void ActnListener(){
//set the text for label 1
lbl1.setText("ActionListener");
//create a scrollpane
ScrollPane pane = new ScrollPane();
//create a button
Button btnn = new Button("Click this");
//add actionlistener
btnn.addActionListener(new CustomActionListener());
//add button to the pane
pane.add(btnn);
//add pane to the panel
pnl.add(pane);
//set visibility as true
fr.setVisible(true);
}
class CustomActionListener implements ActionListener{
//declare the actionperformed method
public void actionPerformed(ActionEvent e) {
//set the text as lbl2
lbl2.setText("Now, the button is clicked");
}}}
Output:
In this program, a button with a label will be exhibited, unlike the above program.
When this button is clicked, an action encounters such that a text will display showing the button is clicked.
Example #3
Code:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class JActionListenerExample {
//create a frame
private Frame fr;
//create a label1
private Label lbl1;
//create a label2
private Label lbl2;
//create a panel
private Panel pnl;
//variable to count the number of clicks
private int noClick = 0;
public JActionListenerExample(){
//call the method userint()
userint();
}
public static void main(String[] args){
//create the object of class
JActionListenerExample jl = new JActionListenerExample();
jl.ActnListener();
}
//definition of method userint
private void userint(){
//frame
fr = new Frame(" ActionListener Example");
//set the size of frame
fr.setSize(300,300);
//set the layout of frame
fr.setLayout(new GridLayout(4, 2));
//add the window listener
fr.addWindowListener(new WindowAdapter() {
//call when window is closing
public void windowClosing(WindowEvent wev){
System.exit(0);
}
});
//create label1
lbl1 = new Label();
//set the alignment
lbl1.setAlignment(Label.CENTER);
//create label2
lbl2 = new Label();
//set the alignment
lbl2.setAlignment(Label.CENTER);
//set the size
lbl2.setSize(450,200);
//create panel
pnl = new Panel();
//set the layout of panel
pnl.setLayout(new FlowLayout());
//add the label1 to frame
fr.add(lbl1);
//add the panel to frame
fr.add(pnl);
//add the label2 to frame
fr.add(lbl2);
//visibility as true
fr.setVisible(true);
}
//method definition
private void ActnListener(){
//set the text for label 1
lbl1.setText("ActionListener");
//create a scrollpane
ScrollPane pane = new ScrollPane();
//set the background for pane
pane.setBackground(Color.green);
//create a button
Button btnn = new Button("Click this");
//add actionlistener
btnn.addActionListener(new CustomActionListener());
//add button to the pane
pane.add(btnn);
//add pane to the panel
pnl.add(pane);
//set visibility as true
fr.setVisible(true);
}
class CustomActionListener implements ActionListener{
//declare the actionperformed method
public void actionPerformed(ActionEvent e) {
//increment the count if button is clicked
noClick++;
//set the text as lbl2
lbl2.setText("The button is clicked "+ noClick + " number of times");
}}}
Output:
In this program also, a button with a label will be exhibited.
But unlike the above program, a text with a count of clicks will also be displayed as shown below.
Conclusion
Java ActionListener is a class that gets notified when the user clicks a menu item or button. More details on this interface are discussed in this document in detail.
Recommended Articles
This is a guide to Java ActionListener. Here we discuss the basic concept, how java ActionListener works along with steps to create ActionListener and its examples. You may also look at the following articles to learn more –