Advanced Java Tutorial

Total available pages count: 55
Subject - Java Technologies

Event Handling

The changing of the state of an object is known as an event & the mechanism to control such events is known as event Handling.

e.g., click on a button, dragging a mouse, scrolling page, selecting an item from a list, etc.

The event can be classified into 2 categories:

Foreground events

The events require direct interaction with the generated as a consequence of the person that interacts with the graphical component in GUI. e.g., clicking the button, Moving the mouse. Enter values using a keyboard, scrolling the page.

Background events

the event requires the interaction with the end-user that is known as a background event. e.g., software or hardware failure, operating system interrupts, the timer expires.

The event Handling mechanism controls the event & decides the action about if an event occurs what will happen. It has the code which is called an event handler that is executed when the event get occurs. The delegation event model uses to handle the event. The model indicates a standard mechanism to generate & handle the events.

Key participant name of Delegation event model:


 An object on which an event occurs. It is responsible for providing the information of the released event to its handler.


It is responsible to generate responses to events. We can say a listener is an object it waits until it receives an event, once an event gets received the listener process the event then returns.

The main benefit of this model is that UI logic is completely get separated from the logic that generates an event. The UI element is able to delegate the processing of events to a separate piece of code. In the model, the listener needs to be registered with the source object so the listener can receive the event notification. It is an effective way to handle the event.

  • Even get generated when the user clicks on the button
  • Automatically Object of the concerned event class gets created & the data about the source & event get executed within the same object
  • The method get executed & returns
  • Event object do forward to the method of the registered listener
  • To design a listener class you have to develop some listener interfaces. The listener interfaces forecast the public abstract callback method which should be implemented by the listener class
  • If you do not implement any of the predefined interfaces then the class can not act as a listener class for the source object