Event Handling and Its Components in Java



The GUI in Java processes the interactions with users via mouse, keyboard, and various user controls such as buttons, checkboxes, text fields, etc., as events. These events are to be handled properly to implement Java as an Event-Driven Programming.

What is Event Handling?

Event handling refers to the mechanism that controls events and determines the actions taken when an event occurs. This mechanism includes code known as an event handler, which is executed in response to an event.

Components in Event Handling

The following are the three main components of event handling in Java:

Events

The events are defined as an object that describes a change in the state of a source object. Java defines a number of such Event Classes inside java.awt.event package.
Some of the events are as follows:

  • ActionEvent: Occurs when a button is clicked or an action is performed.
  • MouseEvent: Triggered by mouse actions (click, drag, move).
  • KeyEvent: Generated when a key is pressed, released, or typed.
  • FocusEvent: Occurs when a component gains or loses focus.
  • ItemEvent: Fired when an item is selected/deselected (e.g., checkbox).

Event Sources

A source is an object that generates an event. An event generation occurs when an internal state of that object changes in some way. A source must register listeners in order for the listeners to receive notifications about a specific type of event.
Some of the event sources are as follows:

  • Button: Generates an ActionEvent when clicked.
  • CheckBox: Generates an ItemEvent on selection change.
  • List: Fire ActionEvent or ItemEvent on item selection.
  • Choice: Triggers ItemEvent when an option is chosen.
  • Window: Shows WindowEvent on close/minimize/maximize.

Event Listeners

A listener is an object that is notified when an event occurs. A Listener has two major requirements: it should be registered to one or more source objects to receive event notifications, and it must implement methods to receive and process those notifications. Java has defined a set of interfaces for receiving and processing the events under the java.awt.event package.

Some of the listeners are as follows:

  • ActionListener: Handles button clicks like actionPerformed().
  • MouseListener: Handles mouse events like mouseClicked() and mousePressed().
  • ItemListener: Manages ItemEvent like checkbox and radio button toggles.
  • KeyListener: Handles keyboard input like keyPressed() and keyReleased().
  • WindowListener: Handles window operations like windowOpened() and windowClosed().

Example for Event Handling

Below is an example to demonstrate events, event sources and event listeners in Java:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class EventListenerTest extends JFrame implements ActionListener {
   JButton button;
   public static void main(String args[]) {
      EventListenerTest object = new EventListenerTest();
      object.createGUI();
   }
   void createGUI() {
      button = new JButton(" Click Me !");
      setSize(300,200);
      setLocationRelativeTo(null);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setVisible(true);
      add(button);
      button.addActionListener(this);
   }
   public void actionPerformed(ActionEvent ae) {
      if(ae.getSource() == button) {
         JOptionPane.showMessageDialog(null, "Generates an Action Event");
      }
   }
}

Output

Alshifa Hasnain
Alshifa Hasnain

Converting Code to Clarity

Updated on: 2025-05-06T18:38:43+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started