Look, Read, Understand, Apply


Event and Event Handling

An event is a signal, like mouse click, key press, window resize etc, received by a program from some source like operating system as user has done some kind of action (key press, mouse click) or something else has happened.

Delegation model: In Java, delegation model is used to handle events.
  • A component (source) registers itself with a source to receive a signal or event notification.
  • As source receives signal, it sends event notification to the registered listener.
  • The listener receives event notification, processes the event and returns.

A listener object may be defined by:

  • Implementing a listener interface
  • Using object of listener interface
For example, Class abc implements ActionListener{ //a class abc implementing to listener interface, } button.addActionListener(this); //button registering to the event handler class, here object of class abc itself, Public void actionPerformed(ActionEvent ae){ //event handler class implements the method in the listener interface to handle an event. }