coderz.py

Keep Coding Keep Cheering!

Event Handling in Java

java thread class

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

Leave a Comment

Your email address will not be published. Required fields are marked *

Advertisement