ADS

Event Handling In Java For Beginners

 Here is a quick tutorial on Event handling in Java for beginners Event Handling in Java for Beginners
Here is a quick tutorial on Event handling in Java for beginners. This tutorial contains every event listener interface and class you need to know.

Introduction

Any action that user performs on a GUI component must be listened and necessary action should to be taken. For example, if a user clicks on a Exit button, then we need to write code to exit the program. So for this, we need to know that the user has clicked the button. This process of knowing is called as listening and the action done by the user is called an event. Writing the corresponding code for a user action is called as Event handling.

An event listener in Java is an interface that contains methods called handlers in which corresponding action code is to be written.
An event class contains the information about an event.
Event source is the GUI component or model on which an event is generated or in other words an action is done.
An adapter class is an abstract class implementing a listener interface. This is essential when we don't want to write all the handlers. For example, MouseListener interface contains a lot of methods such as mousePressed(), mouseReleased().. and we want to write only one of them, we use adapter class. This class implements all the methods of an interface giving them an empty body while itself being abstract.

Dispatching of events

For every action user performs, a corresponding event object is generated. This generated event object should be sent to the corresponding listener so that we can handle that event and write the code accordingly. The process of sending of event object to its corresponding listener is called as event dispatching. Events cannot be dispatched if they aren't generated and an event, except MouseEvent cannot be generated on a disabled component.

Semantic vs Low level events

Low level events represent direct interaction with the user. They represent low level input such as keyboard or mouse. Here are a list of low level events.
 java.awt.event.ComponentEvent
                    |
                    +-- java.awt.event.InputEvent
                    |                |
                    |                +-- java.awt.event.MouseEvent
                    |                +-- java.awt.event.KeyEvent
                    |
                    +-- java.awt.event.FocusEvent
                    |
                    +-- java.awt.event.ContainerEvent
                    |
                    +-- java.awt.event.WindowEvent
Semantic events are dependent events i.e. they depend on low level events. Sources of semantic events can be model like a Timer. Examples include ActionEvent, ItemEvent etc.

Event classes and their hierarchy

A GUI component can be registered to multiple listeners either of the same type or of different types supported by it.
Not all GUI components can generate all types of events. For example, a Frame cannot generate an ActionEvent

Event classes are the heart of event handling. They contain the information about the generated event. We need to learn them before we step into the concept. The AWT defines event classes that are also used in most of the Swing components.
 java.util.EventObject   
        |
        +-- java.awt.event.AWTEvent
                    |
                    +-- ActionEvent
                    +-- AdjustmentEvent
                    +-- AncestorEvent
                    +-- ComponentEvent
                    +-- HierarchyEvent
                    +-- InputMethodEvent
                    +-- javax.swing.event.InternalFrameEvent
                    +-- InvocationEvent
                    +-- ItemEvent
                    +-- TextEvent

The super class of all the events is java.util.EventObject. This class contains getSource() method which returns the source of the generated event. An immediate sub class of EventObject is the AWTEvent class which is the super class of all AWT based events.

Examples on each event

ActionEvent

ActionEvent is generated on various AWT components like Button, TextField etc.
  1. Create ActionListener for AWT Button
  2. Create ActionListener for AWT MenuItem
  3. Using ActionListener for AWT TextField 
  4. Using ActionListener for AWT List 
  5. Using Shortcut for AWT MenuItem

ItemEvent

This event is generated whenever an item is selected/de-selected on a List or a Choice typically.
  1. Using ItemListener for AWT Checkbox
  2. Using ItemListener for AWT RadioButton
  3. Using ItemListener for AWT Choice
  4. Using ItemListener for AWT List

KeyEvent

This event is generated whenever user presses/releases or types a key.
  1. Using KeyListener for AWT TextField
  2. A Funny KeyEvent Prank in Java

TextEvent

This event is generated whenever there is a change in text of a text component such as TextField or TextArea.
  1. Using TextListener for AWT TextField

MouseEvent

This is generated whenever user performs an action with the mouse. These include pressing,releasing,clicking,entering,exiting,moving and dragging of mouse.
  1. Using MouseListener for AWT Frame 
  2. Using MouseMotionListener on AWT Frame

MouseWheelEvent

This is generated whenever user does something with his mouse wheel.
  1. Using MouseWheelListener on AWT Frame

WindowEvent

This is generated whenever there is a change in the window state such as minimized,maximized,activate,de-activated,opened,closing,closed.
  1. Using WindowListener for AWT Frame
  2. Close AWT Frame in Java using WindowListener

FocusEvent

This event is generated when a component gets the focus i.e. it is selected currently.
  1. Using FocusListener on AWT TextField
  2. Using FocusListener on AWT Button

ContainerEvent

This event is generated whenever a component is added or removed to a container.
  1. Using ContainerListener on AWT Frame

ComponentEvent

This event is generated whenever there is a change in the size,location,visibility of a component.
  1. Using ComponentListener for AWT Button

WindowFocusEvent

This event is generated whenever a window gets or loses focus.
  1. Using WindowFocusListener on AWT Frame

AdjustmentEvent

This event is generated whenever there is a value change in a scrollbar.
  1. Using AdjustmentListener for AWT Scrollbar
The above events will give you a core understanding of the AWT Event Handling which pushes you forward to get into the Swing's.
If you have time, refer to the official event handling tutorial in Swing as well.

Subscribe to receive free email updates:

ADS