Computer programs are the logical part which works on the user given data/inputs. In case of Applet/Desktop application, user provides data by using AWT/Swing components which are meant for collecting user data e.g. TextField/TextArea are used to get Text input from user, Menu/Button/Toggle Button are used to get Click related user actions, etc. To grab the data/actions provided by user we have to do “Event Handling”.

What is Event Handing?

Event handling is the process of detecting User Input to application or any component of the application.

Think yourself as a program/application and consider that you have been given a responsibility to detect and handle text event on TextField. How you gonna detect it? Of course, you must be all time waiting for the text! That’s what we call event handling. We would like to make our program all time waiting for certain input. So that when user really feeds that input data, Program detects it and work accordingly.

Hope you have understood the above event handling scenario. Now, we want our program to handle multiple events at the same time. That means program must keep on waiting for multiple kind of user data e.g Key, Mouse, etc. To accomplish this goal we have to follow multithreaded architecture. To detect and handle an event JAVA provides “Event Delegation Model” which is already a multi threaded model and can be directly used by developer.

Event Delegation Model

It is divided into 3 parts:

  1. Event Listeners:
    For each specific event(Like Mouse Click, Mouse Move, Key Press/Release), a Listener Interface is available with us which declares set of methods for the same.
    E.g. java.awt.event.ActionListener
  2. Events:
    Event is an object which holds the data related to the user activity.
    E.g. java.awt.event.ActionEvent
  3. Events Source:
    A component on which we would like to apply the listening.
    E.g. Button, MenuItem

Important Events:

Event Basic

Important Listener:

Listener basic