Programming Pandit

c/c++/c#/Javav/Python


Latest Update

Wednesday, April 16, 2025

AWT EVENT HIERARCHY IN JAVA


The Abstract Window Toolkit (AWT) in Java enables the development of Graphical User Interfaces (GUI). Central to AWT is its event-driven programming model, built on a Delegation Event Model. This model distinguishes between the event source (the object that generates an event) and the event listener (the object that receives and handles the event).

The event-handling mechanism is organized through a structured class hierarchy, originating from the java.util.EventObject class. This hierarchy supports efficient processing of various types of user interactions such as mouse movements, key presses, window operations, and component changes.






🌳 Detailed AWT Event Class Hierarchy


java.util.EventObject


  • Super class for all events in Java (not limited to GUI).
  • Contains the source of the event (i.e., the object that fired the event).

public class EventObject extends Object implements Serializable {

    protected transient Object source;

}


2️ java.awt.AWTEvent

  • Extends EventObject and is the base class for all AWT-specific events.
  • Includes an event ID which uniquely identifies the type of event.
  • All event classes in java.awt.event are indirect subclasses of AWTEvent.

public class AWTEvent extends EventObject {

    protected int id;

}


🔰 Subclasses of AWTEvent (with Listener Interfaces)

🔶 1. ComponentEvent

  • Represents events related to components (shown, hidden, moved, or resized).
  • Superclass for events like FocusEvent, KeyEvent, MouseEvent.

🔸 Common Listener: ComponentListener
🔸 Related Methods: componentResized(), componentMoved(), etc.


🔶 2. ContainerEvent

  • Generated when components are added to or removed from containers.
    🔸 Common Listener: ContainerListener

🔶 3. FocusEvent

  • Occurs when a component gains or loses input focus.
    🔸 Common Listener: FocusListener
    🔸 Events: focusGained(), focusLost()

🔶 4. WindowEvent

  • Generated when a window is opened, closed, minimized, etc.
    🔸 Common Listener: WindowListener
    🔸 Events: windowOpened(), windowClosing(), etc.

🔶 5. ActionEvent

  • Generated by buttons, menus, or text fields when actions are performed.
    🔸 Common Listener: ActionListener
    🔸 Important Method: actionPerformed(ActionEvent e)

🔶 6. ItemEvent

  • Fired when an item (checkbox, list, etc.) is selected or deselected.
    🔸 Common Listener: ItemListener
    🔸 Method: itemStateChanged(ItemEvent e)

🔶 7. AdjustmentEvent

  • Triggered when the value of a scrollbar is adjusted.
    🔸 Common Listener: AdjustmentListener

🔶 8. TextEvent

  • Generated when the content of a text component is modified.
    🔸 Common Listener: TextListener

🔶 9. InputEvent (Abstract Class)

  • Superclass for KeyEvent and MouseEvent.
  • Contains information about modifier keys (ALT, CTRL, SHIFT).

🧷 KeyEvent

  • Triggered when keyboard keys are pressed, released, or typed.
    🔸 Common Listener: KeyListener
    🔸 Methods: keyPressed(), keyReleased(), keyTyped()

🧷 MouseEvent

  • Triggered on mouse actions such as click, enter, exit, press, release.
    🔸 Common Listener: MouseListener
    🔸 Extended by MouseMotionListener and MouseWheelListener

🧷 MouseWheelEvent

  • Fired when mouse wheel is rotated.
    🔸 Common Listener: MouseWheelListener

📊 Event Class to Listener Mapping

Event Class

Listener Interface

Method(s)

ActionEvent

ActionListener

actionPerformed()

ItemEvent

ItemListener

itemStateChanged()

TextEvent

TextListener

textValueChanged()

KeyEvent

KeyListener

keyPressed(), keyReleased()

MouseEvent

MouseListener

mouseClicked(), mousePressed()

MouseEvent

MouseMotionListener

mouseMoved(), mouseDragged()

MouseWheelEvent

MouseWheelListener

mouseWheelMoved()

WindowEvent

WindowListener

windowClosing(), windowOpened()

FocusEvent

FocusListener

focusGained(), focusLost()

ComponentEvent

ComponentListener

componentResized(), etc.

ContainerEvent

ContainerListener

componentAdded(), componentRemoved()

AdjustmentEvent

AdjustmentListener

adjustmentValueChanged()


🎓 Academic Insight: Delegation Event Model

The Delegation Event Model is a scientific approach to event handling that promotes separation of concerns and better modularity.
In this model:

  • The source is the object that generates the event.
  • The listener is the object that receives and processes the event.
  • This model replaces the older inheritance-based approach (used in Java 1.0) and supports multiple listeners for the same event source, thus increasing flexibility.


The AWT Event Hierarchy is an organized and extensible framework that empowers developers to handle various kinds of user interactions in GUI-based applications. Understanding this hierarchy not only enables robust event management but also provides insight into the design of event-driven systems in general. Its abstraction and specialization allow for scalable and maintainable GUI architecture in Java applications.

 

No comments:

Post a Comment