In Java, an event is an object which specifies the change of state in the source. It is generated whenever an action takes place like a mouse button is clicked or text is modified. Java’s AWT (Abstract Window Toolkit) is responsible for communicating these actions between the program and the user. Java packages such as java. util, java. awt, java. awt. event and javax. swing support event handling mechanism.
When an action takes place, an event is generated. The generated event and all the information about it such as time of its occurrence, type of event, etc. are sent to the appropriate event handling code provided within the program. This code determines how the allocated event will be handled so that an appropriate response can be sent to the user.
Event Handling Model
The working of an event-driven program is governed by its underlying event-handling model. Till now two models have been introduced in Java for receiving and processing events. The event handling mechanisms of these models differ a lot from each other.
Java 1.0 Event Model
The Java 1.0 Event model for event processing was based on the concept of containment. In this approach, when a user-initiated event is generated it is first sent to the component in which the event has occurred. But in case the event is not handled at this component, it is automatically propagated to the container of that component. This process is continued until the event .is processed or it reaches the root of the containment hierarchy.
For example, as shown in the Figure, Button is contained in the Panel which itself is contained within the Frame. When the mouse is clicked on Button, an event is generated which is first sent to the Button. If it is not handled by it then this event is forwarded to the Panel and if it cannot handle the event, it is further sent to the Frame. Frame being the root of the given hierarchy processes this event. So the event is forwarded up the containment hierarchy until it is handled by a component.
The major drawback in this approach is that events could be handled by the component that generated it or by the container of that component. Another problem is that events are frequently sent to those components that cannot process them, thus wasting a lot of CPU cycles.
Delegation Event Model
The advanced versions of Java ruled out the limitations of Java 1.0 event model. This model is referred to as the Delegation Event Model which defines a logical approach to handle events. It is based on the concept of source and listener. A source generates an event and sends it to one or more listeners. On receiving the event, listener processes the event and returns it. The notable feature of this model is that the source has a registered list of listeners which will receive the events as they occur. Only the listeners that have been registered actually receive the notification when a specific event is generated.
For example, as shown in Figure, when the mouse is clicked on Button, an event is generated. If the Button has a registered listener to handle the event, this event is sent to Button, processed and the output is returned to the user. However, if it has no registered listener the event will not be propagated upwards to Panel or Frame.
Now we discuss Event Source and Event Listener in detail.
• Event source: An event source is an object that generates a particular kind of event. An event is generated when the internal state of the event source is changed. A source may generate more than one type of event. Every source must register a list of listeners that are interested to receive the notifications regarding the type of event. Event source provides methods to add or remove listeners.
The general form of method to register (add) a listener is:
public void addTypeListener(TypeListener eventlistener)
Similarly, the general form of method to unregister (remove) a listener is:
public void removeTypeListener(TypeListener eventlistener)
Type is the name of the event
eventlistener is a reference to the event listener
• Event listener: An event listener is an object which receives notification when an event occurs. As already mentioned, only registered listeners can receive notifications from sources about specific types of events. The role of event listener is to receive these notifications and process them.