articles

Event & Listener in Java

Anupam Mishra 3682 08-Dec-2015

For example, we have click on button, enter the data through the keyboard, dragging mouse etc. i.e. generated an event. Then we say that, changing the state of an object is called as an event.

The java.awt.event package provides many event classes and Listener interfaces for event handling.

For example, the Button fires off an ActionEvent whenever the user presses it. The entire point of an event is to inform a listener that something has happened to a component in the GUI. An event includes all of the information that a listener needs to figure out what happened and to whom it happened (the what and who of the event). An event must give enough information to fully describe itself. That way, a listener can figure out what exactly happened and respond in a meaningful way.

Following steps are required to perform event handling:

1.    Implement the Listener interface and overrides its methods

2.    Register the component with the Listener

For registering the component with the Listener, many classes provide the registration methods. For example:

Button

public
      void addActionListener(ActionListener a){
          }

JTextField

public void addActionListener(ActionListener a){
        }
public void addTextListener(TextListener a){}

How to use event handling code in java program:

We can put the event handling code into one of the following places:

1.    In Same class

2.    In Outer class/Other Class

3.    Anonymous class

1. Same Class

 import java.awt.*;
import java.awt.event.*;  import javax.swing.*;
class EventWithInClass extends JFrame implements ActionListener{ 
 
EventWithInClass(){ 
super("Event With In Class ");
Button b=new Button("click Here"); 
b.setBounds(100,120,80,30);   
b.addActionListener(this);   
add(b);
setSize(300,300); 
setLayout(null); 
setVisible(true);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation(dim.width/2-this.getSize().width/2, dim.height/2-this.getSize().height/2);
         

 
public void actionPerformed(ActionEvent e){ 
JOptionPane.showMessageDialog(null, "Welcome to you on MindStick Software Pvt. Ltd.");

 
public static void main(String args[]){ 
new EventWithInClass();  } 
}

Output:  Is Below


2. Other Class: 
import java.awt.*;  
import java.awt.event.*;    import javax.swing.*;
class OuterClass extends JFrame { 
 
OuterClass (){ 
super("Event With In Class ");
Button b=new Button("click Here"); 
b.setBounds(100,120,80,30);  Outer o=new Outer(this);  b.addActionListener(o);//passing outer class instance   
 
add(b);
setSize(300,300); 
setLayout(null); 
setVisible(true);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation(dim.width/2-this.getSize().width/2, dim.height/2-this.getSize().height/2);
         

public static void main(String args[]){ 
new OuterClass (); 
}
class Outer implements ActionListener{ 
OuterClass obj; 
Outer(OuterClass obj){  this.obj=obj; 

public void actionPerformed(ActionEvent e){ 
 JOptionPane.showMessageDialog(null, "Welcome to you on MindStick Software Pvt. Ltd.");

}   
}

Output:  Is Below

 

 

3. Anonymous class: 
import java.awt.*;  
import java.awt.event.*;  import javax.swing.*;
public class AnonymousClass extends JFrame { 
public AnonymousClass (){  super("Event With In Class ");
Button b=new Button("click Here"); 
b.setBounds(100,120,80,30);  b.addActionListener(new ActionListener(){
        @Override
            public void actionPerformed(ActionEvent e) {
                    JOptionPane.showMessageDialog(null, "Welcome to you on  MindStick Software Pvt. Ltd.");
            }
        }); 
add(b);
setSize(300,300); 
setLayout(null); 
setVisible(true);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation(dim.width/2-this.getSize().width/2, dim.height/2-this.getSize().height/2);
 } 
public static void main(String args[]){ 
new AnonymousClass ();  }
}
Output:

Output is same for all programs

Before Click:-

 

Event & Listener in Java

After Click:-

 

Event & Listener in Java

Updated 07-Sep-2019

Leave Comment

Comments

Liked By