Home Java Tutorial Java Event Handling

Java Event Handling

by anupmaurya
2 minutes read

What is an Event?

An event in Java is an object that is created when something changes within a graphical user interface. If a user clicks on a button, clicks on a combo box, or types characters into a text field, etc., then an event triggers, creating the relevant event object. This behavior is part of Java’s Event Handling mechanism .

What is Event Handling?

Event Handling is the mechanism that controls the event and decides what should happen if an event occurs. This mechanism have the code which is known as event handler that is executed when an event occurs. 

How Events Work

Event handling in Java is comprised of two key elements:

  • The event source, which is an object that is created when an event occurs. Java provides several types of these event sources
    •  Types of Events 
      1. ActionEvent: Represents a graphical element is clicked, such as a button or item in a list. Related listener: ActionListener.
      2. ContainerEvent: Represents an event that occurs to the GUI’s container itself, for example, if a user adds or removes an object from the interface. Related listener: ContainerListener.
      3. KeyEvent: Represents an event in which the user presses, types or releases a key. Related listener: KeyListener.
      4. WindowEvent: Represents an event relating to a window, for example, when a window is closed, activated or deactivated. Related listener: WindowListener.
      5. MouseEvent: Represents any event related to a mouse, such as when a mouse is clicked or pressed. Related listener: MouseListener.
  • The event listener, the object that “listens” for events and processes them when they occur.

You may also like

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.