←Back to Subject
Notes
Notes
Study material and explanations
Diagrams
Diagrams
Visualize concepts easily
Mnemonics
Mnemonics
Tricks to remember faster
One Night Prep
One Night Prep
Short and focused revision
Revision
Revision
Quick summary and key points
Important Questions
Important Questions
Practice most relevant questions
Notes

The Java Event Handling Model: Event Delegation Model & Event Class Hierarchy

Study material and explanations for Unit 4

The Java Event Handling Model: Event Delegation Model & Event Class Hierarchy

1. What is an Event and Event Handling?

  • Event: Any change in the state of a GUI component caused by user interaction—such as clicking a Button, pressing a keyboard key, moving the mouse, selecting an item from a Choice menu, or closing a Frame window.
  • Event Handling: The mechanism that controls an event and decides what action should execute when that event occurs. In Java, all event handling classes and interfaces are located inside the java.awt.event package.

2. Java’s Event Delegation Model (Modern Approach — JDK 1.1+)

In the old Java 1.0 Event Model, whenever you clicked a button, the event travelled up the entire containment hierarchy until some component handled it, or every unhandled event wasted CPU cycles!

To fix this, Java introduced the Event Delegation Model.

  • Core Idea: Instead of the GUI component (Button) handling its own click event, the Event Source (Button) delegates (forwards) the responsibility of handling the event to a separate registered object called an Event Listener!
Figure 4.1: How the Event Delegation Model Works in Java
1. Event SourceGUI Component (e.g., Button) registers a Listener via addActionListener()
→ User Clicks →
2. Event ObjectSource creates ActionEvent object & sends it
→ Calls Method →
3. Event ListenerExecutes actionPerformed(ActionEvent e)

The 3 Core Participants of the Event Delegation Model:

  1. Event Source (Generator): The GUI component (Button, Checkbox, TextField, Scrollbar, Frame) where the user interaction actually happens.
    • An Event Source must register listeners using a method like source.addTypeListener(listenerObj) (e.g., btn.addActionListener(this);).
  2. Event Object (Message): When the user interacts with the Event Source, the source creates an Event Object (such as ActionEvent, MouseEvent, or KeyEvent) that encapsulates all details about the event (which button was clicked, (x, y) coordinates of the mouse, timestamp) and passes it as a parameter to the Listener.
  3. Event Listener (Handler): An object that implements a specific Listener interface (e.g., ActionListener, MouseListener). It waits until it receives the Event Object from the Event Source and then executes its event-handling method (e.g., actionPerformed(ActionEvent e)).

3. Ignoring the Event, Self-Contained Events & Delegating Events

Your DAVV Unit 4 syllabus specifically mentions three ways a Java GUI program can deal with events:

1. Ignoring the Event

  • In the Event Delegation Model, if no listener is registered with an Event Source (for example, the user moves the mouse over a Frame, or clicks a Button on which addActionListener() was never called), the event is completely ignored and discarded!
  • Benefit: Unlike Java 1.0, zero CPU time is wasted firing events that the programmer does not care about.

2. Self-Contained Events (Component Handling Its Own Event)

  • In this approach, the GUI component class (Frame or a subclass of Button) acts as BOTH the Event Source and its own Event Listener!
  • For example, your main class MyFrame extends Frame implements ActionListener registers itself using btn.addActionListener(this); and handles the click inside the very same class.
  • When to use: Best for small, simple BCA lab programs with 1 or 2 buttons.

3. Delegating Events (Separate Handler Class)

  • In large real-world applications, putting 50 button handlers inside a single Frame class makes the code messy.
  • In Delegating Events, the Frame (UI View) delegates event handling to a completely separate helper class (or an Inner Class / Anonymous Class) that implements the Listener interface:
    // Separate class handles the click event!
    btnSubmit.addActionListener(new SubmitButtonHandler());
  • Benefit: Cleanly separates GUI Design (View) from Business Logic (Controller).

4. The Event Class Hierarchy in Java

Every event class in Java inherits from the root class java.util.EventObject, which has a key method getSource() (returns the object that generated the event).

Inside java.awt, AWTEvent is the superclass of all AWT event classes:

Figure 4.2: The Java Event Class Hierarchy (java.util.EventObject → java.awt.AWTEvent)
java.util.EventObject (getSource())  →  java.awt.AWTEvent (getID())
Direct Semantic Subclasses of AWTEvent
• ActionEvent (Button click, Menu)
• ItemEvent (Checkbox, Choice)
• AdjustmentEvent (Scrollbar)
• TextEvent (TextField/TextArea)
ComponentEvent & Its Subclasses
• ContainerEvent (Component added/removed)
• FocusEvent (Gained/lost focus)
• WindowEvent (Window opened/closed)
• InputEvent → KeyEvent & MouseEvent

5. Complete Working Example of the Event Delegation Model

import java.awt.*;
import java.awt.event.*; // Required for Event classes and Listener interfaces

// Self-Contained Event: Class extends Frame AND implements ActionListener
public class EventDelegationDemo extends Frame implements ActionListener {
    Button btnClick;
    Label lblStatus;

    public EventDelegationDemo() {
        setTitle("Event Delegation Model Demo");
        setSize(350, 200);
        setLayout(new FlowLayout());

        btnClick = new Button("Click Me!");
        lblStatus = new Label("Waiting for button click...");

        add(btnClick);
        add(lblStatus);

        // STEP 1: Register the Event Source (btnClick) with the Listener (this)
        btnClick.addActionListener(this);

        setVisible(true);
    }

    // STEP 2: Implement the Listener interface method
    @Override
    public void actionPerformed(ActionEvent e) {
        lblStatus.setText("Hurray! Button was clicked successfully!");
    }

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

💡 Exam Preparation Tips for DAVV BCA

  • Always draw the 3-part Event Delegation Model diagram (Event Source → Event Object → Event Listener) in any Unit 4 Event Handling question.
  • Remember the 3 steps to write any Event Handling program:
    1. import java.awt.event.*;
    2. Implement the appropriate Listener interface (implements ActionListener).
    3. Register the component using component.addXxxListener(this) and override the handler method!