Event Handling - Button Click

Learn Developing button click event handling

Java follows Event Delegation Model for handling all kind of events. When an action is performed on button, a object of corresponding event is generated. This event object is sent to only registered listeners.

In this tutorial we will learn attaching of listener to Button object.

Event Handling for Button
  • Implement ActionListener interface and define actionPerformed method
  • For registering create object of class which implemented ActionListener interface.
  • Call addActionListener() method and pass obejct created in step 2.

Create Listener

In order to create event listener for button click event, ActionListener interface has to be implemented . This interface has one method actionPerformed(), which need to be defined in implemented class.


class handler implements ActionListener
{
 public void actionPerformed(ActionEvent ae)
  {
   // This code will be executed when button click event generated.
    System.out.println("Button Clicked");
  }
}  

Register Listener to Button

After creating listener we need to register this listener to button object for which we want to handle event. For registering event listener, addActionListener() method is called. This method need one argument of listener class.


JButton jb=new JButton("Click");

// Registering Event listener with object.
handler h=new handler();
jb.addActionListener(h); 

Program

/* 
   Event handling Simple Program
   www.jgyan.com
*/
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
class firstFrame
 {
  public static void main(String arg[])
   {
    JFrame jf=new JFrame("First Frame");
    jf.setSize(400,400);
	
	JButton jb=new JButton("Click");
	// Setting layout null
	jf.setLayout(null);
	
	// Settting Position and size of button
	jb.setBounds(30,30,90,30);
	
	// Adding object in frame
	jf.add(jb);
	
	// Registering Event listener for Button 
	handler h=new handler();
	jb.addActionListener(h);
	
    jf.setVisible(true);
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   }
 }
 class handler implements ActionListener
  {
   public void actionPerformed(ActionEvent ae)
    {
	  System.out.println("Button Clicked");
	}
  }
 
 

Explanation

In above program we simply created a frame and one button. Layout for frame set as null, it means frame is not using any layout manager. In case when no layout manager is used, setBounds() used for specifying position and size of component.

class handler implemented ActionListener interface and defined actionPerformed() method. On button click event actionPerformed() method got executed. So code which we need to execute on click event, has to be written inside the actionPerformed() method.

Object of handler class is created and this object is passed in addActionListener() method for register listener.


getActionCommand() method

Same listener can be attached to multiple components. In such scenario we need some mechanism to find out which component is triggering event.

Let's say we have a frame with two button YES and CANCEL. Now if we add same listener with these two button then how we differentiate when YES button is click and when CANCEL button is clicked. To resolve this situation getActionCommand() method of ActionEvent class is used.


public void actionPerformed(ActionEvent ae)
 {
  // com will store label of button which is generating event
   String com=ae.getActionCommand();
   
   System.out.println(com+" button clicked");
 }

Multiple buttons with same Listener

/* 
   Event handling Simple Program
   www.jgyan.com
*/
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
class clickEvent
 {
  public static void main(String arg[])
   {
    JFrame jf=new JFrame("First Frame");
    jf.setSize(400,400);
	
	JButton jb=new JButton("YES");
	JButton jb1=new JButton("NO");
	JButton jb2=new JButton("CANCEL");
	
	// Setting layout null
	jf.setLayout(null);
	
	// Settting Position and size of button
	jb.setBounds(30,30,90,30);
	jb1.setBounds(150,30,80,30);
	jb2.setBounds(250,30,80,30);
	
	// Adding object in frame
	jf.add(jb);
	jf.add(jb1);
	jf.add(jb2);
	
	// Registering Event listener for Button 
	handler h=new handler();
	jb.addActionListener(h);
	jb1.addActionListener(h);
	jb2.addActionListener(h);
	
    jf.setVisible(true);
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   }
 }
 class handler implements ActionListener
  {
   public void actionPerformed(ActionEvent ae)
    {
	  String com=ae.getActionCommand();
	  System.out.println(com+" Button Clicked");
	}
  }