Categories: Java

Event Handling in Java

An event is defined as the act of changing the state of an item or behavior. A button click, cursor movement, key press via keyboard, or page scrolling are all examples of actions.

Various event classes can be found in the java.awt.event package.

Event Handling Components:

There are three major components to event management.

  • An event is a change in the state of an item.
  • An object that generates an event is referred to as an event source.
  • Listeners are objects that pay attention to an event. When an event occurs, a listener is notified.
How are Events Handled?

A source generates an Event and sends it to one or more registered listeners. When the listener receives an event, it processes it and returns. Several Java packages, including java.util, java.awt, and java.awt.event, support events.

Event Classes in Java:
Event ClassListener InterfaceDescription
ActionEventActionListenerAn event that signals the occurrence of a component-defined action, such as a button click or the selection of an item from the menu-item list.
AdjustmentEventAdjustmentListenerAn Adjustable object, such as a Scrollbar, emits the adjustment event.
ComponentEventComponentListenerAn Adjustable object, such as a Scrollbar, emits the adjustment event. An event that indicates that a component has moved, its size has changed, or its visibility has changed.
ContainerEventContainerListenerWhen a component is added to (or withdrawn from) a container, a container object generates this event.
FocusEventFocusListenerThese are events connected to attention, such as focus, focusin, focusout, and blur.
ItemEventItemListenerAn event that indicates whether or not an item was selected.
KeyEventKeyListenerAn event that occurs as a result of a series of keystrokes on the keyboard.
MouseEventMouseListener & MouseMotionListenerThe events that occur as a result of a user’s engagement with the mouse (Pointing Device).
MouseWheelEventMouseWheelListenerAn event that indicates that the mouse wheel in a component was rotated.
TextEventTextListenerAn event occurs when the text of an object changes.
WindowEventWindowListenerAn event that indicates whether a window’s status has changed.
Listener Interface & their methods:
Listener InterfaceMethods
ActionListeneractionPerformed()
AdjustmentListeneradjustmentValueChanged()
ComponentListenercomponentResized()
componentShown()
componentMoved()
componentHidden()
ContainerListenercomponentAdded()
componentRemoved()
FocusListenerfocusGained()
focusLost()
ItemListeneritemStateChanged()
KeyListenerkeyTyped()
keyPressed()
keyReleased()
MouseListenermousePressed()
mouseClicked()
mouseEntered()
mouseExited()
mouseReleased()
MouseMotionListenermouseMoved()
mouseDragged()
MouseWheelListenermouseWheelMoved()
TextListenertextChanged()
WindowListenerwindowActivated()
windowDeactivated()
windowOpened()
windowClosed()
windowClosing()
windowIconified()
windowDeiconified()
Procedure for dealing with events:
  • In the class, implement the proper interface.
  • Notify the listener about the component.

Note: Example of event handling already seen in the previous post.

Note: also read about the JApplet class in Applet

Follow Me

If you like my post, please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

2 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

1 year ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

1 year ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

1 year ago