Javatpoint Logo
Javatpoint Logo

Types of events in Java

An event is one of the most important concepts in Java. The change in the state of an object or behavior by performing actions is referred to as an Event in Java. Actions include button click, keypress, page scrolling, or cursor movement.

Java provides a package java.awt.event that contains several event classes.

We can classify the events in the following two categories:

  1. Foreground Events
  2. Background Events
Types of events in Java

Foreground Events

Foreground events are those events that require user interaction to generate. In order to generate these foreground events, the user interacts with components in GUI. When a user clicks on a button, moves the cursor, and scrolls the scrollbar, an event will be fired.

Background Events

Background events don't require any user interaction. These events automatically generate in the background. OS failure, OS interrupts, operation completion, etc., are examples of background events.

Delegation Event Model

A mechanism for controlling the events and deciding what should happen after an event occur is referred to as event handling. Java follows the Delegation Event Model for handling the events.

The Delegation Event Model consists of Source and Listener.

Source

Buttons, checkboxes, list, menu-item, choice, scrollbar, etc., are the sources from which events are generated.

Listeners

The events which are generated from the source are handled by the listeners. Each and every listener represents interfaces that are responsible for handling events.

To learn more about Delegation Event Model, go through the following link:

https://www.javatpoint.com/delegation-event-model-in-java

We need to register the source with the listener for handling events. Different types of classes provide different registration methods.

The syntax of registering the source with the listener is as follows:

For example, if we need to register Key and Action events, we use the addActionListener() and addKeyListener() methods.

These are some of the most used Event classes:

S.No. Event Class Listener Interface Methods Descriptions
1. ActionEvent ActionListener actionPerformed() ActionEvent indicates that a component-defined action occurred.
2. AdjustmentEvent AdjustmentListener adjustmentValueChanged() Adjustment events occur by an adjustable object like a scrollbar.
3. ComponentEvent ComponentListener componentResized(), componentMoved(), componentShown() and componentHidden() An event occurs when a component moved, changed its visibility or the size changed.
4. ContainerEvent ContainerListener componentRemoved() and componentAdded() The event is fired when a component is added or removed from a container.
5. FocusEvent FocusListener focusLost() and focusGained() Focus events include focus, focusout, focusin, and blur.
6. ItemEvent ItemListener itemStateChanged() Item event occurs when an item is selected.
7. KeyEvent KeyListener keyPressed(), keyReleased(), and keyTyped(). A key event occurs when the user presses a key on the keyboard.
8. MouseEvent MouseListener and MouseMotionListener mouseClicked(), mousePressed(), mouseEntered(), mouseExited() and mouseReleased() are the mouseListener methods. mouseDregged() and mouseMoved() are the MouseMotionListener() methods. A mouse event occurs when the user interacts with the mouse.
9. MouseWheelEvent MouseWheelListener mouseWheelMoved(). MouseWheelEvent occurs when the mouse wheel rotates in a component.
10. TextEvent TextListener textChanged() TextEvent occurs when an object's text change.
11. WindowEvent WindowListener windowActivated(), windowDeactivated(), windowOpened(), windowClosed(), windowClosing(), windowIconfied() and windowDeiconified(). Window events occur when a window's status is changed.

Let's take an example to understand how we can work with the events and listeners:

EventHandlingExample1.java

Output:

Types of events in Java





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA