Different ways to Handle Event - Action Event with Examples

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:



****************
Previous Lessons;

Post a Comment

0 Comments