Curriculum
Course: Learn Java Programming
Login

Curriculum

Learn Java Programming

Text lesson

Overview of Event Handling Concept in Java

[post-views]

 

 

In this lesson, you will learn.

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

 

Introduction to Event Delegation Model

  1. The Event Delegation Model defines a standard and consistent mechanism to generate and process 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 an event is received, the listener processes the event and then returns.

 

In the delegation event model, the listeners must register with a source to receive an event notification so that they are sent only to listeners wanting to receive 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 a question being asked, 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

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

 

2. Event Sources

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

 

Here is the syntax to register listeners to receive an event notification.

 public void addTypeListener (TypeListener el )

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

 

For example, the method that registers 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.

  1. First, it must have been registered with one or more sources to receive notifications about specific types of events.
  2. 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 the information regarding the event.

The information may contain the source that generated the event and the time when the event was generated.

 

4. Event Handlers

An event handler is a method that is invoked by an event listener when the event is generated. 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

  1. 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.
  2. For example, when the key is pressed from 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 are discussing some of the event classes in Java.

 

Event Classes

S.N Event Class Description
1 ActionEvent Generated when a button is pressed, a list item is double-clicked, or a menu item is selected.
2 ItemEvent Generated when a check box or list item is clicked.
3 KeyEvent Generated when input is received from the keyboard.
4 MouseEvent Generated when the mouse is dragged, moved, clicked, pressed, or released. Also generated when the mouse enters or exits a component.
5 MouseWheelEvent Indicates that the mouse wheel was rotated.
6 TextEvent Represents an event fired by a text field or text area to indicate that its text has changed.
7 WindowEvent Represents events that occur with window actions (e.g., opened, closed, activated, deactivated).

 

The EventListener (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 is responsible for handling the button-click event.

 

Here, we are discussing some of the event classes in Java.

 

The Event Listeners

Listener Interface Description Key Methods
ActionListener Listens for action events (e.g., button clicks). void actionPerformed(ActionEvent e)
ItemListener Listens for item events (e.g., checkbox state changes). void itemStateChanged(ItemEvent e)
KeyListener Listens for key events (e.g., key presses and releases). void keyPressed(KeyEvent e),
void keyReleased(KeyEvent e),
void keyTyped(KeyEvent e)
MouseListener Listens 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)
MouseMotionListener Listens for mouse motion events (e.g., mouse moves, drags). void mouseDragged(MouseEvent e)
void mouseMoved(MouseEvent e)
MouseWheelListener Listens for mouse wheel events. void mouseWheelMoved(MouseWheelEvent e)
WindowListener Listens 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)
TextListener Listens for text events (e.g., text field changes). void textValueChanged(TextEvent e)

 

 

 

 


 

End of the lesson….enjoy learning

 

 

Student Ratings and Reviews

 

 

 

There are no reviews yet. Be the first one to write one.

 

 

Submit a Review

 

 

Layer 1
Login Categories