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 Class | Listener Interface | Description |
---|---|---|
ActionEvent | ActionListener | An 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. |
AdjustmentEvent | AdjustmentListener | An Adjustable object, such as a Scrollbar, emits the adjustment event. |
ComponentEvent | ComponentListener | An 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. |
ContainerEvent | ContainerListener | When a component is added to (or withdrawn from) a container, a container object generates this event. |
FocusEvent | FocusListener | These are events connected to attention, such as focus, focusin, focusout, and blur. |
ItemEvent | ItemListener | An event that indicates whether or not an item was selected. |
KeyEvent | KeyListener | An event that occurs as a result of a series of keystrokes on the keyboard. |
MouseEvent | MouseListener & MouseMotionListener | The events that occur as a result of a user’s engagement with the mouse (Pointing Device). |
MouseWheelEvent | MouseWheelListener | An event that indicates that the mouse wheel in a component was rotated. |
TextEvent | TextListener | An event occurs when the text of an object changes. |
WindowEvent | WindowListener | An event that indicates whether a window’s status has changed. |
Listener Interface & their methods:
Listener Interface | Methods |
---|---|
ActionListener | actionPerformed() |
AdjustmentListener | adjustmentValueChanged() |
ComponentListener | componentResized() componentShown() componentMoved() componentHidden() |
ContainerListener | componentAdded() componentRemoved() |
FocusListener | focusGained() focusLost() |
ItemListener | itemStateChanged() |
KeyListener | keyTyped() keyPressed() keyReleased() |
MouseListener | mousePressed() mouseClicked() mouseEntered() mouseExited() mouseReleased() |
MouseMotionListener | mouseMoved() mouseDragged() |
MouseWheelListener | mouseWheelMoved() |
TextListener | textChanged() |
WindowListener | windowActivated() 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
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Leave a Comment