Curriculum
Course: Complete Java Programming Course
Login

Curriculum

Complete Java Programming Course

Video lesson

Overview of Event Handling Concept in Java

In this lesson, you will learn.

  • Introduction of the Event Delegation Model
  • Understanding the Event Classes
  • Examples

 

Introduction to Event Delegation Model

  1. The Event Delegation Model defines a standard, consistent mechanism for generating and processing events.
  2. A source generates an event and sends it to one or more listeners.
  3. In this scheme, the listener waits until it receives an event. Once it has been received, the listener processes it and then returns.

 

In the delegation event model, listeners must register with a source to receive event notifications, which are sent only to listeners who want them.

 

Let’s use a classroom scenario to explain the Java Event Delegation model.

Ref: https://in.pinterest.com/pin/school-children-group-and-young–447263806749671861/

 

Imagine a classroom where the teacher (Mr. Smith) is giving a lecture, and the students are listening. Mr. Smith has an assistant (Alice) who helps him manage the class.

In this analogy:

  1. Teacher (Mr. Smith): This represents the event source. Mr. Smith is the one who triggers events, such as starting a lecture or asking a question.
  2. Students: These represent the event listeners. The students are interested in certain events, such as the start of a lecture or the asking of a question, and they respond accordingly.
  3. Assistant (Alice): Alice represents the event handler. Instead of the teacher directly interacting with each student, he delegates the task of notifying the students to Alice.

 

Components of Event Handling

The following are the 4 important components of the Event Handling Model.

1. Events

  • An event is an object that describes a state change in a source.
  • For example, when interacting with graphical interfaces, events can be generated by pressing a button, entering a character via the keyboard, selecting an item from a list, or clicking the mouse.

     

    2. Event Sources

    • A source is an object that generates an event. This occurs when the internal state of that object changes. Sources may generate more than one type of event.
    • source object must register listeners to receive notifications for a specific event type.

       

      Here is the syntax for registering listeners to receive event notifications.

       public void addTypeListener (TypeListener el )

      Here, Type is the event name, and el is a reference to the event listener.

       

      For example, the method used to register a keyboard event listener is called addKeyListener(). The method that registers a mouse listener is called addMouseListener( ).

      When an event occurs, all registered listeners are notified and receive a copy of the event object.

      This is known as multicasting the event. In all cases, notifications are sent only to listeners who register to receive them.

       

      3. Event Listeners

      A listener is an object that is notified when an event occurs. It has two major requirements.

      • First, it must have been registered with one or more sources to receive notifications about specific types of events.
      • Second, it must implement methods to receive and process these notifications.

         

        Once the listener is notified of a specific event, it calls the event handler (methods) and passes information about the event.

        The information may include the source that generated the event and the time it was generated.

         

        4. Event Handlers

        An event handler is a method invoked by an event listener when an event is triggered. These methods are defined in the Interfaces and found in java.awt.event package.

        For example, the MouseMotionListener interface defines two methods to receive notifications when the mouse is dragged or moved.

         

        Understanding the Event Classes

        • Event classes represent the event characteristics, such as the event’s source, time, and ID. Whenever an event occurs, the related object of the event class is created.
        • For example, when the key is pressed on the keyboard, an object of the KeyEvent class is created. When the mouse is clicked, an object of the MouseEvent class is created.

           

          Here, we discuss some event classes in Java.

           

          Event Classes in Java

          S.NEvent ClassDescription
          1ActionEventGenerated when a button is pressed, a list item is double-clicked, or a menu item is selected.
          2ItemEventGenerated when a check box or list item is clicked.
          3KeyEventGenerated when input is received from the keyboard.
          4MouseEventGenerated when the mouse is dragged, moved, clicked, pressed, or released. Also generated when the mouse enters or exits a component.
          5MouseWheelEventIndicates that the mouse wheel was rotated.
          6TextEventRepresents an event fired by a text field or text area to indicate that its text has changed.
          7WindowEventRepresents events that occur with window actions (e.g., opened, closed, activated, deactivated).

           

          The Event Listener (Interfaces)

          1. The listener interface is defined in java.awt.event package.
          2. An event listener provides the handler methods used to handle events.
          3. For example, when a button is clicked, the actionPerformed() method of the ActionListener interface handles the button-click event.

          Here, we discuss some event classes in Java.

           

          The Event Listeners

          Listener InterfaceDescriptionKey Methods
          ActionListenerListens for action events (e.g., button clicks).void actionPerformed(ActionEvent e)
          ItemListenerListens for item events (e.g., checkbox state changes).void itemStateChanged(ItemEvent e)
          KeyListenerListens for key events (e.g., key presses and releases).void keyPressed(KeyEvent e),
          void keyReleased(KeyEvent e),
          void keyTyped(KeyEvent e)
          MouseListenerListens for mouse events (e.g., clicks, presses, releases).void mouseClicked(MouseEvent e)
          void mousePressed(MouseEvent e)
          void mouseReleased(MouseEvent e)
          void mouseEntered(MouseEvent e)
          void mouseExited(MouseEvent e)
          MouseMotionListenerListens for mouse motion events (e.g., mouse moves, drags).void mouseDragged(MouseEvent e)
          void mouseMoved(MouseEvent e)
          MouseWheelListenerListens for mouse wheel events.void mouseWheelMoved(MouseWheelEvent e)
          WindowListenerListens for window events (e.g., open, close, activate, deactivate).void windowOpened(WindowEvent e)
          void windowClosing(WindowEvent e)
          void windowClosed(WindowEvent e)
          void windowIconified(WindowEvent e)
          void windowDeiconified(WindowEvent e)
          void windowActivated(WindowEvent e)
          void windowDeactivated(WindowEvent e)
          TextListenerListens for text events (e.g., text field changes).void textValueChanged(TextEvent e)