Skip to content

Event-Handling

Java uses the Delegation Event Model for handling events, which separates event generation from event handling.

  1. Event Source:
    1. An object generates an event when an action occurs.
    1. Example: A button click generates an event.
  2. Event Listener:
    1. An object that wants to receive notifications of events.
    1. Must implement a listener interface and register itself with the source.
  3. Event Notification:
    1. The source notifies registered listeners by calling appropriate methods in the listener interface.

Steps for Event Handling

  1. Implement the Listener Interface:
    1. Create a class that implements one or more event listener interfaces.
  2. Register the Listener:
    1. Register the listener object with the event source using methods like addActionListener().
  3. Handle the Event:
    1. Define the logic inside the listener’s method to handle the event.

Important Event Listener Interfaces

InterfacePurpose
ActionListenerHandles action events like button clicks.
KeyListenerHandles keyboard events like key presses.
MouseListenerHandles mouse click, press, release, enter, and exit events.
MouseMotionListenerHandles mouse movement and drag events.
WindowListenerHandles window events like opening, closing, minimizing.
ItemListenerHandles item events like selecting a checkbox.
FocusListenerHandles focus gained and lost events.

Example: Handling Button Click Events

Code Example:

import java.awt.*;

import java.awt.event.*;

public class ButtonClickExample extends Frame implements ActionListener {

    Button button;

    // Constructor

    public ButtonClickExample() {

        // Create a button

        button = new Button(“Click Me”);

        button.setBounds(50, 100, 80, 30);

        // Register the listener

        button.addActionListener(this);

        // Add button to frame

        add(button);

        // Frame settings

        setSize(200, 200);

        setLayout(null);

        setVisible(true);

    }

    // Handle the button click event

    @Override

    public void actionPerformed(ActionEvent e) {

        button.setLabel(“Clicked!”);

    }

    public static void main(String[] args) {

        new ButtonClickExample();

    }

}


Example: Handling Mouse Events

Code Example:

import java.awt.*;

import java.awt.event.*;

public class MouseEventExample extends Frame implements MouseListener {

    Label label;

    // Constructor

    public MouseEventExample() {

        // Create a label

        label = new Label();

        label.setBounds(20, 50, 150, 20);

        // Register the listener

        addMouseListener(this);

        // Add label to frame

        add(label);

        // Frame settings

        setSize(300, 200);

        setLayout(null);

        setVisible(true);

    }

    // Implement mouse listener methods

    @Override

    public void mouseClicked(MouseEvent e) {

        label.setText(“Mouse Clicked”);

    }

    @Override

    public void mouseEntered(MouseEvent e) {

        label.setText(“Mouse Entered”);

    }

    @Override

    public void mouseExited(MouseEvent e) {

        label.setText(“Mouse Exited”);

    }

    @Override

    public void mousePressed(MouseEvent e) {

        label.setText(“Mouse Pressed”);

    }

    @Override

    public void mouseReleased(MouseEvent e) {

        label.setText(“Mouse Released”);

    }

    public static void main(String[] args) {

        new MouseEventExample();

    }

}


Advantages of Event Handling

  1. Separation of Concerns:
    1. The delegation model separates event sources and listeners, making code modular and reusable.
  2. Flexibility:
    1. Multiple listeners can handle events from a single source or vice versa.
  3. Scalability:
    1. Supports large-scale GUI applications with numerous events and interactions.

Limitations of Event Handling

  1. Complexity:
    1. Managing multiple event sources and listeners can lead to intricate code.
  2. Performance:
    1. Handling numerous events in real-time may affect performance in resource-constrained systems.
  3. Verbose Code:
    1. Requires implementing multiple interfaces, leading to boilerplate code.

Modern Alternatives

Java Swing and JavaFX provide more advanced and flexible event-handling mechanisms compared to the basic AWT event model. These frameworks are preferred for modern Java GUI applications.


Conclusion Event handling in Java is a robust mechanism that enables developers to create interactive and responsive GUI applications. By understanding the delegation event model and the key listener interfaces, developers can build scalable and user-friendly applications effectively.