Different ways to Handle Event - Action Event with Examples
Table of Contents
a) Introduction
b) Different ways of handling event
c) Examples with Source Code
d) Output
**********************
Introduction
There are three different ways in which you can
handle the event. Handling the event means interacting with the UI (User
Interface) Elements. We already discussed about the Event Delegation Model
(EDM). Event Delegation Model is a approach of handling event which is based on
the concept of three objects, they are Event Source, Event and Event Listener.
Three Different ways of handling event are:
a) In Same Class
b) Using Inner Class
c) Using Anonymous Inner Class
In Same Class:
It is done in the same class in which the event is
to handle. For example;
Class A implements ******Listener{
    A(){
      b1.add*****Listener(this);
    }
      Public static void
main String(args []){
    new A();
    }
       public void
*********(****Event e){
     //statements
    }
}
Let’s see the full program and output using Action
Listener;
/*
 * To change this license
header, choose License Headers in Project Properties.
 * To change this template
file, choose Tools | Templates
 * and open the template
in the editor.
 */
package EventHandling;
/**
 *
 * @author AnkitPC
 */
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SameClassEventHandlingDemo implements
ActionListener{
   JFrame f;
   JButton b1,b2;
   JTextField t;
  Â
  Â
SameClassEventHandlingDemo(){ //constructor
       f=new JFrame("Same
Class Event Handling");
      Â
f.setSize(400,300);
      Â
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
       f.setLayout(new
FlowLayout());
      Â
       b1=new
JButton("Hello"); //button one
       b2=new
JButton("Clear"); // button two
       t=new
JTextField(20); // 20 is the width of the text field
      Â
      Â
b1.addActionListener(this); // to perform a action when hello button is
clicked
      Â
b2.addActionListener(this);
      Â
       f.add(t);
       f.add(b1);
       f.add(b2);
      Â
      Â
f.setVisible(true);
      Â
   }
   //main method
   public static void
main(String[] args) {
       new
SameClassEventHandlingDemo();
      Â
   }
  Â
   public void
actionPerformed(ActionEvent e){
      Â
       /*
       if a user clicks
button one i.e. b1 then display hello in the text field and if user clicks the
button two
       then clear the
text field.
       */
      Â
       // since there are
two button i.e b1 and b2 so we have to know which button is clicked so
      Â
if(e.getSource()==b1){ //the varable of ActionEvent provides the button
clcked
          Â
t.setText("Hello"); //writes hello in textfield
       }
       else
if(e.getSource()==b2){
          Â
t.setText(""); // clears the textfield
       }
   }
          Â
  Â
}
Output:
Using Inner Class:
It is done in two different classes in the same
class.
class A(){
    A(){
     b.add******Listener(new B());
    }
    public static void
main(String [] args){
         new A();
    }
class B implements ****** Listener{
Â
Public void **********(****Event e){
 }
}
}
Let’s see the full program and output using Action
Listener;
/*
 * To change this license
header, choose License Headers in Project Properties.
 * To change this template
file, choose Tools | Templates
 * and open the template
in the editor.
 */
package EventHandling;
/**
 *
 * @author AnkitPC
 */
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class InnerClassEventHandlingDemo{
   JFrame f;
   JButton b1,b2;
   JTextField t;
  Â
      Â
InnerClassEventHandlingDemo(){
       f=new
JFrame("Same Class Event Handling");
      Â
f.setSize(400,300);
      Â
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
       f.setLayout(new
FlowLayout());
      Â
       b1=new
JButton("Hello"); //button one
       b2=new
JButton("Clear"); // button two
       t=new
JTextField(20); // 20 is the width of the text field
      Â
      Â
b1.addActionListener(new A()); // to perform a action when hello button
is clicked
      Â
b2.addActionListener(new A());
      Â
       f.add(t);
       f.add(b1);
       f.add(b2);
      Â
      Â
f.setVisible(true);
   }
  Â
   public static void
main(String[] args) {
    new
InnerClassEventHandlingDemo ();Â
   }
   class A implements ActionListener { // inner class
      Â
   public void actionPerformed(ActionEvent e){
      Â
       /*
       if a user clicks
button one i.e. b1 then display hello in the text field and if user clicks the
button two
       then clear the
text field.
       */
      Â
       // since there are
two button i.e b1 and b2 so we have to know which button is clicked so
      Â
if(e.getSource()==b1){ //the varable of ActionEvent provides the button
clcked
          Â
t.setText("Hello World"); //writes hello world in textfield
       }
       else
if(e.getSource()==b2){
          Â
t.setText(""); // clears the textfield
       }
   }
  }
}
Output:
Using Anonymous Inner Class:
class A{
    A(){
     b.add***Listener(new ***Listener(){
         public void
******(****Event e){
             //statements
         }
    });
    public static void main(String [] args){
    new A();
    }
}
Let’s see the full program and output using Action
Listener;
/*
 * To change this license
header, choose License Headers in Project Properties.
 * To change this template
file, choose Tools | Templates
 * and open the template
in the editor.
 */
package EventHandling;
/**
 *
 * @author AnkitPC
 */
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class AnynomousClassEventHandlingDemo{
    JFrame f;
   JButton b1,b2;
   JTextField t;
  Â
  Â
AnynomousClassEventHandlingDemo(){ //constructor
       f=new
JFrame("Same Class Event Handling");
      Â
f.setSize(400,300);
      Â
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
       f.setLayout(new
FlowLayout());
      Â
       b1=new
JButton("Hello"); //button one
       b2=new
JButton("Clear"); // button two
       t=new
JTextField(20); // 20 is the width of the text field
      Â
       // to perform a
action when hello button is clicked
      Â
b1.addActionListener(new ActionListener(){
           public void
actionPerformed(ActionEvent e){
              Â
t.setText("Hello"); //writes hello in textfield
           }
       });
      Â
b2.addActionListener(new ActionListener(){
           public void
actionPerformed(ActionEvent e){
              Â
t.setText(""); //writes hello in textfield
           }
       });
      Â
       f.add(t);
       f.add(b1);
       f.add(b2);
      Â
      Â
f.setVisible(true);
      Â
   }
  Â
   //main method
   public static void
main(String[] args) {
       new
AnynomousClassEventHandlingDemo();
   }
}
Output: