Java | Event Handling

05 min read

Event Handling:

An event in an Applet is an object that represents some action such as clicking the mouse, pressing a key on the keyboard, closing window, pressing a button, etc.

Objects (e.g. Buttons, Text Fields, mouse, etc) are said to generate or lire events. Different types of objects can tire different kinds of events. For example,

  • Button and TextField objects fire ActIonEvent objects
  • a mouse can generate a MouseEvent
  • a window can generate a WIndowEvent

Every object that can fire an event is called an event source. A Listener is an object that is notified when an event occurs

 

There are two Event handling models

1) inheritance  or hierarchical model:

If an event occurs, the event would be propagated to all the components, up the containment hierarchy until it is handled by a component. This required components to receive even the events that they do not process and it wastes time.

2)Delegation event model:

A listener simply waits until it receives an event. When a source generates an event and sends it to one or more listeners, the listener receives it, processes it and then returns. In this, the listeners must register with a source in order to receive an event notification. Thus notifications are sent only to the listeners that want to receive them. A listener object has methods that specify what will happen when an event is sent to the listener. These methods are called event handlers. The programmers have to define these event handlers.

 

To perform event handling the following two steps have to be done.

  • The Listeners must register with 1 or more sources to receive notifications about specific types of events
  • must implement event handling methods to receive & process these notifications.

 

How to register Listeners?

Each type of event has its own registration method.

Syntax : public void addTypeListener(TypeListener ref)

Where types should be the name of the event and ref is the reference to the event listener.

Eg: The method that registers a keyboard event listener ---> addKeyListener( )

The method that registers a  mouse motion listener ---> addMouseMotionListener( )

When an event occurs, all registered listeners are notified & receive a copy of the event object. Listeners are created by implementing 1 or more of the interfaces defined by the java.awt.event.*package. So, to use the event handling in the application, import the java.awt.event.* package.

Whenever an event is fired from a component, an event object is propagated to an appropriate method in the Listener object, and that method contains code to handle the event. Corresponding to every AWT Event there is a corresponding event listener. For example for ActionEvent we have ActionListener. There is a method in ActionListener interface actionPerformed() to which the ActionEvent object is passed and this method can be defined to handle the event.

POST A NEW COMMENT
     
  • Input (stdin)

    Output (stdout)


    Input (stdin)

    Your Output (stdout)

    Expected Output

    Compiler Message

    Input (stdin)

    2    3

    Your Output (stdout)

    5

    Expected Output

    5

    Compiler Message

    5

    Error