---
title: "Event & Listener in Java"  
description: "If you are excited to know how to  swing component event & listener are working then this article is very important"  
author: "Anupam Mishra"  
published: 2015-12-08  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/11937/event-listener-in-java  
category: "java"  
tags: ["java"]  
reading_time: 3 minutes  

---

# Event & Listener in Java

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](https://www.mindstick.com/articles/26/interfaces) for [event handling](https://www.mindstick.com/articles/60/event-handling-in-csharp-dot-net).

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](https://www.mindstick.com/articles/12262/learn-how-to-make-a-component-in-magento-2) 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](https://yourviews.mindstick.com/story/1909/6-meaningful-ways-to-celebrate-christmas-this-year) way.

##### Following steps are required to perform event handling:

1. Implement the Listener [interface](https://www.mindstick.com/articles/12036/interface-in-c-sharp) and overrides its methods

2. [Register](https://www.mindstick.com/articles/12423/social-login-magento-2-one-click-to-register-social) the component with the Listener

For registering the component with the Listener, many classes provide the [registration](https://answers.mindstick.com/qa/116207/what-is-lei-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](https://www.mindstick.com/blog/11108/nested-classes-in-java-anonymous-classes) ##### 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](https://www.mindstick.com/mindstickarticle/ed328cb7-cb2e-4df1-9f1e-02ad94691fbd/images/877a3dd2-a0b9-4963-b2e5-3d942f402429.png)
>
> ## After Click:-
>
> ![Event & Listener in Java](https://www.mindstick.com/mindstickarticle/ed328cb7-cb2e-4df1-9f1e-02ad94691fbd/images/e38ab029-a9f7-484f-91d3-3631876911fc.png)

---

Original Source: https://www.mindstick.com/articles/11937/event-listener-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
