Java ActionListener Interface

The Java ActionListener is informed at whatever point you click on the button or menu item. It is told against ActionEvent. The ActionListener interface is found in java.awt.event package. It has just a single strategy: actionPerformed().

actionPerformed() method

The actionPerformed() method is invoked automatically whenever you click on the registered component.

  1. public abstract void actionPerformed(ActionEvent e);  

Java ActionListener Example: On Button click

import java.awt.*;  
  • import java.awt.event.*;  
  • public class ActionListenerExample {  
  • public static void main(String[] args) {  
  •     Frame f=new Frame("ActionListener Example");  
  •     final TextField tf=new TextField();  
  •     tf.setBounds(50,50150,20);  
  •     Button b=new Button("Click Here");  
  •     b.setBounds(50,100,60,30);  
  •     b.addActionListener(new ActionListener(){  
  •     public void actionPerformed(ActionEvent e){  
  •             tf.setText("Welcome to Javatpoint.");  
  •     }  
  •     });  
  •     f.add(b);f.add(tf);  
  •     f.setSize(400,400);  
  •     f.setLayout(null);  
  •     f.setVisible(true);   
  • }  
  • }  

  • javacodegeeks is optimized for learning.© javacodegeeks .
    All Right Reserved and you agree to have read and accepted our term and condition