Event Handling in Java

 

🌟 Event Handling in Java

Event Handling is the mechanism that controls the events and defines how a program responds to user interactions such as:

  • Mouse clicks

  • Keyboard input

  • Window actions (e.g., closing a window)

  • Button presses

Java uses the Delegation Event Model to handle these interactions in GUI applications.


🎯 1. Event Handling Mechanism

Java’s event handling is based on three components:

  1. Event Source – the component (e.g., button) that generates the event.

  2. Event Object (Event Class) – encapsulates information about the event.

  3. Event Listener (Interface) – receives and processes the event.

Java’s model is known as the Delegation Event Model, where the source delegates the task of handling the event to the listener.


🔄 2. Delegation Event Model

The Delegation Event Model is Java's standard for handling events. It has three main participants:

  • Source: Component that generates events (e.g., JButton)

  • Event Object: Encapsulates the event (e.g., ActionEvent)

  • Listener: Interface that must be implemented to handle the event (e.g., ActionListener)

🔧 Steps in the Delegation Model:

  1. A component is registered with an event listener using methods like .addActionListener().

  2. When the event occurs, the component delegates the event to the listener.

  3. The listener processes the event by executing the code in its method (e.g., actionPerformed()).


📦 3. Event Classes (Event Objects)

Java provides a set of event classes in the java.awt.event and javax.swing.event packages.

Event ClassDescription
ActionEvent                Generated when a button is clicked
MouseEvent                Mouse movements and clicks
KeyEvent                Keyboard key press/release
WindowEvent                Window open/close/minimize
ItemEvent                Checkbox or radio button selection change
AdjustmentEvent                Scrollbar adjustments
TextEvent                Text field changes
FocusEvent                When a component gains/loses focus

💡 4. Event Sources

Sources are components like:

  • Button, JButton

  • TextField, JTextField

  • Checkbox, JCheckBox

  • Window, JFrame, JDialog

Each source has methods like:

  • addActionListener()

  • addMouseListener()

  • addKeyListener()


🎧 5. Event Listener Interfaces

Java provides listener interfaces that must be implemented to handle events. Here are some commonly used listeners:

Listener InterfaceMethod to ImplementUsed With
ActionListener    actionPerformed(ActionEvent e)        Buttons, menus
MouseListener    mouseClicked(), mousePressed(), etc.        Mouse events
KeyListener    keyPressed(), keyReleased(), etc.        Keyboard
ItemListener    itemStateChanged(ItemEvent e)        Checkbox, combo box
WindowListener    windowClosing(), etc.        Windows
FocusListener    focusGained(), focusLost()            Components with focus

🛠️ 6. Using the Delegation Event Model – Example

import java.awt.*;
import java.awt.event.*; import javax.swing.*; public class EventDemo extends JFrame implements ActionListener { JButton btn; public EventDemo() { btn = new JButton("Click Me"); btn.addActionListener(this); // Registering the event listener add(btn); setSize(300, 200); setLayout(new FlowLayout()); setVisible(true); setDefaultCloseOperation(EXIT_ON_CLOSE); } // Event handler public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(this, "Button Clicked!"); } public static void main(String[] args) { new EventDemo(); } }

🔍 How It Works:

  • JButton is the event source.

  • ActionEvent is the event object.

  • The class implements ActionListener and overrides actionPerformed().

  • The listener is registered using addActionListener(this).


✅ Summary Table

ComponentRole
Event Source                        Triggers event (e.g., Button)
Event Object                        Stores event details (e.g., ActionEvent)
Event Listener                        Responds to event (e.g., ActionListener)
Delegation Model                        Source delegates event to registered listener

Comments

Popular posts from this blog

Object Oriented Programming PBCST304 KTU BTech CS Semester 3 2024 Scheme - Dr Binu V P

Introduction to Java Programming

Inheritance and Polymorphism