The Change in state of an object is called as an event i.e. the event describes a particular change in the state of a source.
The Events are generated as a result of the user interaction with Graphical User Interface (GUI) components.
For example- Clicking on a button, movement of the mouse, inserting a character through the keyboard, selection of an item from the given list, scrolling of the page are all of the activities which causes an event to be happen.
Types of Event
The events can be generally classified into following two categories:
1) The Foreground Events:-
- The event which requires the direct interaction of the user are called as the Foreground events.
- They are generated as a consequence of a user interacting with the graphical components in GUI. For example- Clicking on a button, movement of the mouse, inserting a character through the keyboard, selection of an item from the given list, scrolling of the page.
- The Background Events :-
- The event that requires the interaction of an end user is called as the Background events.
- For Example:- an Operating system interrupts, failure of hardware or software, a timer expires, an operation completion etc. are the examples of Background events.
Now Event Handling is the mechanism that has control over the event and decides what should be happen if an event occurs.
This mechanism has the separate code which is known as event handler which is executed when an event happens.
The Java uses a Delegation Event Model to handle all the events which defines the standard mechanism to generate & handle the events.
Let’s have a look on the brief introduction to this model.
The Delegation Event Model has following key participants as:
- Source – The source is an object on which an event occurs and the Source is responsible for providing information of the event which has occurred to it’s handler. Java provide it as with classes for the source object.
- Listener – It is also called as event handler. The Listener is responsible for generating a response to an event. From the java implementation point of view the listener is an object too. The Listener waits until it has received an event and Once the event is received , the listener process the event & then returns the result.
Following are the Steps to Handle Events
- To Declare and instantiate the event sources (or components) as buttons, menus, choices etc.
- To Implement an interface (listener) for providing the event handler that responds to event source activity.
- To Register this event handler with event source.
- To Add the event source with the container like frame, panel etc.