Java – Event Handling4 min read


An event is an object that describes the change of state in a source. It is the changes made within the GUI(Graphical User Interface). The activities that cause the event to be generated are the pressing of a button, selecting items, input the information, clicking of the mouse, etc, basically all the interaction with GUI.

Two types of Event:

  • Foreground Events:
    It is the clicking of a mouse, pressing the button, input character from the keyboard. scrolling of the page etc. That is, these are events where the user is to interact directly with the GUI.
  • Background Events:
    The end-user interaction is seen in this type of event such as OS interruption, the timer expires, operation completion, etc. This is not directly caused by interactions with a user interface.

Event Handling:

It is a mechanism that handles all the events occurring and also controls the further process when an event occurs.
To handle the events, Java uses the Delegation Event Model.

Let us learn about the Delegation Event Model.

Delegation Event Model:

This model in Java is a new approach to handle the events. It is the standard and consistent way to Handle the events generated by the interaction with GUI components.

The terms used:

  • Event Source:
    A source is an object that generates a specific kind of event. This may be the GUI component such as button list, Window, etc. Source provides data of the event that is occurred to the handler.
    A listener gets notified about the type of event that occurred and for that source must register a listener.
    The general form to register the listener to an event source is:
    publicvoid addTypeListener(TypeEvent e)
    Here, Type is the name of the event and e is the reference of the event listener.
  • Event Listener:
    A listener is an object that gets notified when an event occurs. In order to get notified, it must be registered to one or more event sources, and also it must implement methods to be able to receive and process it.
    These methods in Listener is found in, or need to import:
    import java.awt.event.*;
  • Process:
    Once the listener is registered, it waits for the event to occur. And when an event occurs, the source object is created and notified to the listener by the listener methods. Then, when the listener receives that object from the source, it executes the event and returns.

Event Class:

Java provides a number of event classes.
Some of them are:

  • ActionEvent:
    It is the easiest and most common event occurring. To write an Action Listener extends a class AWTEvent.
    Example: when a button is pressed, the item in a list is double-clicked, etc.
  • KeyEvent:
    It extends a class InputEvent. It has three methods in the Listener interface.
    public void keyTyped(KeyEvent e)
    {active when typed}
    public void KeyPressed(KeyEvent e)
    {active when pressed}
    public void KeyReleased(KeyEvent e)
    {active when released}
  • MouseEvent:
    It extends a class InputEvent. It has 5 methods.
    It is instantiated when a mouse button is pressed, released, or clicked, or when a mouse cursor enters or exits a visible part of a component.
  • WindowEvent:
    It extends a class InputEvent. It provides 8 methods. Starts from when a Window object is opened, closed, activated, deactivated, iconified, deiconified, or gain or loss of Window Focus.
  • TextEvent:
    It extends a class AWTEvent. This class is instantiated when the text value or text area is changed.
  • ItemEvent:
    It extends AWtEvent. And instantiated when an item or from a list or a checkbox is selected or deselected.

There are more event class such as ComponentEvent, InputEvent, FocusEvent, MouseMotionEvent etc.

EventListener Interfaces:

EventListener Interface class is defined by the java.util.package.
It is an interface where every listener interface has to be extended.

Class Declaration:

The Following shows the declaration for java.util.EventListener interface:

AWT Event Listener Interfaces:

The most commonly used event Listeners are:

  1. ActionListener:
    used for receiving the action events.
  2. AdjustmentListener:
    used for receiving the adjustment events.
  3. ComponentListener:
    used to define when a component is hidden, moved, resized, or shown.
  4. Focus Listener:
    used for receiving the focus events such as when a component loses or gains keyboard focus.
  5. KeyListener:
    used for receiving the key events such as the state of pressed, released, or typed.
  6. TextListener:
    used to receive text events like when the value of text changes.
  7. MouseMotionListener:
    used to receive the mouse motion events like when the mouse is moved or dragged.
  8. ContainerListener:
    used to receive container events like the addition or removal of the component from the container.
  9. WindowListener:
    used to receive window events such as window activation, deactivation, closed, opened or exit.
  10. MouseListener:
    used to receive mouse events such as clicking, pressed or released, etc.
  11. ItemListener:
    used to receive the item events that is the state of change of items.


C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

C program to find factorial of a number using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …

String Pattern Programs in C

In this tutorial, we will write various C pattern programs for String. Before that, you may go through the following topics in C. for loop …