Table of Contents
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
- ActionEvent: Represents a graphical element is clicked, such as a button or item in a list. Related listener: ActionListener.
- 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.
- KeyEvent: Represents an event in which the user presses, types or releases a key. Related listener: KeyListener.
- WindowEvent: Represents an event relating to a window, for example, when a window is closed, activated or deactivated. Related listener: WindowListener.
- MouseEvent: Represents any event related to a mouse, such as when a mouse is clicked or pressed. Related listener: MouseListener.
- Types of Events
- The event listener, the object that “listens” for events and processes them when they occur.