INDEX
CONTENTS PAGE NO
1 Introduction 1
2 Components 2-3
3 Mouse Event Handling 3-9
3.1 MouseListner Interface
3.2 MouseMotionListner
4 Working Of Delegation Event Model 10
5 Advantages 11
6 Disadvantages 12
7 Conclusion 13
8 Questions 14
9 References 14
Delegation Event Model-Handling Mouse Event
Delegation Event Model-Handling Mouse Event
1. Introduction :
The Delegation Event Model is the standard and most widely used event
handling mechanism in Java. It is designed to handle user interactions in GUI (Graphical User
Interface) applications such as clicking a mouse, pressing a button, typing from the keyboard,
or interacting with windows and other components.
In Java, whenever a user performs an action on a component, an event is generated. This event
is an object that contains information about what action has occurred. The component on which
the action is performed is called the event source. For example, a button click, mouse
movement, or key press can act as an event source.
Once the event is generated, it is not handled directly by the source itself. Instead, it is sent to
a separate object called a listener. The listener is responsible for receiving the event and
performing the required task. This listener must be registered with the event source in advance
so that it can respond when the event occurs.
The main idea of the Delegation Event Model is that the responsibility of handling events is
“delegated” from the source to the listener. This separation makes the program more organized
and easier to manage because the event generation code and event handling code are written
separately.
This model also improves code reusability, scalability, and flexibility in Java GUI
programming. Multiple listeners can be registered for a single event source, and a single
listener can handle multiple event sources.
Overall, the Delegation Event Model plays a very important role in making Java applications
interactive, responsive, and user-friendly.
Srinivas Institute of Technology 1
Delegation Event Model-Handling Mouse Event
2. Components of Delegation Event Model
The Delegation Event Model in Java has three main components that work
together to handle user interactions in GUI applications.
2.1 Event Source
The Event Source is the object that generates or triggers an event when a user
performs an action.
• It is the GUI component where the event happens.
• Examples include buttons, text fields, frames, checkboxes, or mouse actions.
• The event source is responsible for creating the event object and sending it to the
listener.
Example:
When a user clicks a button, the button is the event source.
2.2 Event Object
The Event Object is an object that contains all the details about the event.
• It is created automatically when an event occurs.
• It stores information like:
o Type of event (click, key press, mouse move)
o Source of event
o Time of event
• It is passed from the event source to the listener.
Example:
If a button is clicked, the event object may contain:
“Button clicked at coordinates (x, y)”.
Srinivas Institute of Technology 2
Delegation Event Model-Handling Mouse Event
2.3 Event Listener
The Event Listener is an object that receives and handles the event.
• It waits for an event to occur.
• It defines the action to be performed when the event happens.
• It must be registered with the event source.
Example:
When a button is clicked, the listener performs an action like:
• Displaying a message
• Opening a new window
• Processing input
3. Mouse Event Handling in Java
Mouse event handling is the process of detecting and responding to mouse actions such as
clicking, pressing, releasing, moving, or dragging on a component.
When a mouse action occurs, Java generates a MouseEvent and sends it to the registered
listener.
How Mouse Event Handling Works
1. User performs a mouse action (click, move, drag, etc.)
2. Java generates a MouseEvent
3. Event is sent to the registered listener
4. Listener processes the event and executes the required code
Java provides two main interfaces for mouse event handling:
• MouseListener
• MouseMotionListener
Srinivas Institute of Technology 3
Delegation Event Model-Handling Mouse Event
3.1 MouseListener Interface
The MouseListener interface in Java is used to handle basic mouse events such as clicking,
pressing, releasing, entering, and exiting a GUI component like a button, frame, or panel.
It is part of [Link] package and helps make GUI applications interactive.
When a mouse action happens, Java automatically creates a MouseEvent object and calls the
appropriate method of MouseListener.
Methods of MouseListener Interface
The MouseListener interface contains 5 important methods:
1 mouseClicked()
The mouseClicked() method is called when the user presses and releases the mouse button on
the same component. It represents a complete mouse click action. This method is triggered only
after both the press and release actions are completed. It is commonly used when a user selects
an item or clicks a button to perform an action such as submitting a form or opening a file.
2 mousePressed()
The mousePressed() method is called immediately when the mouse button is pressed down on
a component. It does not wait for the mouse button to be released. This method is useful when
we need to detect the start of an action, such as beginning a drag operation or responding
instantly when a button is pressed.
3. mouseReleased()
The mouseReleased() method is called when the mouse button is released after being pressed.
It indicates the end of a mouse click or drag operation. This method is useful for completing
actions that started with mousePressed(), such as finishing a selection or ending a drag-and-
drop action.
4. mouseEntered()
The mouseEntered() method is called when the mouse pointer enters the area of a component.
It detects when the cursor moves over a GUI component like a button or panel. This method is
often used to provide visual effects such as changing the color of a button when the mouse
hovers over it.
Srinivas Institute of Technology 4
Delegation Event Model-Handling Mouse Event
5. mouseExited()
The mouseExited() method is called when the mouse pointer leaves the area of a component.
It detects when the cursor moves out of a GUI component. This method is commonly used to
remove hover effects or reset the appearance of a component back to its original state.
Program
import [Link].*;
import [Link].*;
public class MouseListenerDemo extends Frame implements MouseListener
{
Label l; MouseListenerDemo()
{
l = new Label("Perform Mouse Actions"); [Link](50, 50, 250, 30);
add(l); addMouseListener(this); setSize(400, 300);
setLayout(null);
setVisible(true);
}
public void mouseClicked(MouseEvent e)
{
[Link]("Mouse Clicked");
}
public void mousePressed(MouseEvent e)
{
[Link]("Mouse Pressed");
}
public void mouseReleased(MouseEvent e)
{
[Link]("Mouse Released");
Srinivas Institute of Technology 5
Delegation Event Model-Handling Mouse Event
}
public void mouseEntered(MouseEvent e)
{
[Link]("Mouse Entered");
}
public void mouseExited(MouseEvent e)
{
[Link]("Mouse Exited");
}
public static void main(String args[])
{
new MouseListenerDemo();
}
}
Output:
| |
| Perform Mouse Actions |
| |
| |
| |
Mouse click → “Mouse Clicked”
Mouse press → “Mouse Pressed” Mouse release → “Mouse Released”
Mouse enter → “Mouse Entered”
Mouse exit → “Mouse Exited”
Srinivas Institute of Technology 6
Delegation Event Model-Handling Mouse Event
3.2 MouseMotionListener Methods
The MouseMotionListener is an interface in Java used to handle mouse movement
events. It detects how the mouse moves on a GUI component such as a frame, button, or
panel. It is part of the [Link] package and helps make applications more interactive
by tracking cursor movement.
When a mouse movement occurs, Java automatically creates a MouseEvent object and sends
it to the registered MouseMotionListener.
Methods of MouseMotionListener
The MouseMotionListener interface contains 2 important methods:
1. mouseDragged()
The mouseDragged() method is called when the user presses and holds a mouse button and
moves the mouse at the same time. This method is mainly used for continuous movement
actions. It helps in applications where objects need to be moved or drawn on the screen while
dragging. For example, in drawing applications like MS Paint, this method is used to draw lines
or shapes by dragging the mouse. It is also useful in drag-and-drop operations where items are
moved from one place to another while holding the mouse button.
2. mouseMoved()
The mouseMoved() method is called when the mouse is moved without pressing any mouse
button. It tracks simple cursor movement across the screen or component. This method is useful
when we need to detect the position of the mouse pointer or perform actions when the cursor
moves. For example, it can be used to show tooltips, display coordinates of the mouse position,
or change the appearance of a component when the mouse moves over it without clicking.
Srinivas Institute of Technology 7
Delegation Event Model-Handling Mouse Event
Program
import [Link].*; import [Link].*;
public class MouseMotionDemo extends Frame implements MouseMotionListener
{
Label l; MouseMotionDemo()
{
l = new Label("Move or Drag Mouse"); [Link](50, 50, 200, 30);
add(l); addMouseMotionListener(this);
setSize(400, 300);
setLayout(null);
setVisible(true);
}
public void mouseMoved(MouseEvent e)
{
[Link]("Mouse Moved");
}
public void mouseDragged(MouseEvent e)
{
[Link]("Mouse Dragged");
}
public static void main(String args[]){ new MouseMotionDemo();
}
}
Srinivas Institute of Technology 8
Delegation Event Model-Handling Mouse Event
Output :
| |
| Move or Drag Mouse |
| |
▪ When you move the mouse → "Mouse Moved“
▪ When you drag the mouse (click + move) → "Mouse Dragged"
Srinivas Institute of Technology 9
Delegation Event Model-Handling Mouse Event
4. Working of Delegation Event Model
1. User performs a mouse action:
When a user interacts with a GUI component using a mouse, such as clicking, pressing,
releasing, entering, or moving the cursor over a component, that interaction is called a mouse
action. This is the first step where the user triggers an event in the application by using the
mouse on a specific component like a button, frame, or panel.
2. The source generates a mouse event:
After the user performs a mouse action, the component on which the action happened (called
the event source) generates an event automatically. For example, if the user clicks a button,
that button becomes the source and creates a mouse event to represent that action. This event
contains information like the type of action and position of the mouse.
3. Java creates a MouseEvent object:
Once the event is generated, Java converts it into a MouseEvent object. This object stores all
the details about the mouse action, such as x and y coordinates, click count, and which mouse
button was used. It acts as a message container that carries event information from the source
to the listener.
4. The event is sent to the registered listener:
The generated MouseEvent object is then sent to the listener that has been registered for that
component. A listener is an object that is waiting to receive events. Only those listeners that
are registered will receive the event; others will ignore it. This ensures proper event handling
in the program.
5. The listener processes the event:
Finally, the registered listener receives the event and executes the appropriate method to
handle it, such as mouseClicked(), mousePressed(), or mouseMoved(). In this step, the actual
response to the user action is performed, like updating the UI, showing a message, or
performing some operation.
Srinivas Institute of Technology 10
Delegation Event Model-Handling Mouse Event
5. Advantages
1. Provides efficient event handling
The Delegation Event Model provides efficient event handling because it processes events in
a structured and optimized way. When a user performs an action, only the registered listener
responds to that event. This avoids unnecessary processing and ensures that system resources
are used effectively. As a result, Java GUI applications respond quickly and work smoothly.
2. Separates event handling code from main program
This model separates the event handling code from the main application logic. The event
generation is handled by the source, while the response is handled by the listener. This
separation makes the program cleaner and easier to understand. It also helps developers focus
on application logic without mixing it with event-handling code.
3. Supports multiple listeners
The Delegation Event Model allows multiple listeners to be registered for a single event source.
This means that when one event occurs, more than one listener can respond to it. This feature
increases flexibility and allows different actions to be performed for the same event, making
the application more powerful and dynamic.
4. Improves code organization
This model improves the overall structure and organization of the code. Since event handling
is written separately from the main program, the code becomes modular and well-structured. It
is easier to read, debug, and maintain. Developers can also reuse event-handling components
in different parts of the program.
5. Makes GUI programming flexible and easy
The Delegation Event Model makes GUI programming flexible and easy to implement.
Developers can easily add or remove event listeners without changing the main program logic.
This flexibility allows easy modification and expansion of applications, making Java GUI
development more user-friendly and scalable.
Srinivas Institute of Technology 11
Delegation Event Model-Handling Mouse Event
6. Disadvantages
1. Complex for beginners:
The Delegation Event Model is difficult for beginners to understand because it includes event
source, event object, and listener concepts. New learners may get confused about how events
are generated and handled.
2. Requires multiple listener interfaces:
Different events need different listener interfaces like MouseListener, KeyListener, and
ActionListener. This makes learning and implementing event handling more difficult.
3. Increases code length:
Each event requires separate methods to be written, so even small programs become lengthy.
This increases the overall size of the code.
4. Can cause memory overhead:
When many listeners are added to components, it uses more memory. This can slightly reduce
the performance of large applications.
5. Difficult to manage in large programs:
In big applications, handling many events becomes complex. It is difficult to manage and
track multiple listeners in a single program.
7,Conclusion:
Mouse event handling in Java using the Delegation Event Model provides an efficient way
to detect and respond to user mouse actions. It uses event listener interfaces such as
MouseListener and MouseMotionListener to handle different types of mouse events like
clicking, pressing, releasing, moving, and dragging. This mechanism makes Java GUI
applications interactive, responsive, and easy to manage by separating the event source from
the event handling code.
Srinivas Institute of Technology 12
Delegation Event Model-Handling Mouse Event
8. Questions:
✔ 2 Marks Questions (Delegation Event Model & Mouse Events)
1. What is Delegation Event Model in Java?
2. Define event source.
3. Define event object.
4. What is event listener?
5. Name any two mouse event methods.
6. What is MouseListener?
7. What is MouseMotionListener?
8. Write any two methods of MouseListener.
9. Write any two methods of MouseMotionListener.
10. What is mouse event handling?
✔ 5 Marks Questions (Delegation Event Model & Mouse Events)
1. Explain Delegation Event Model in Java with neat diagram.
2. Explain components of Delegation Event Model.
3. Explain mouse event handling in Java.
4. Explain MouseListener methods with example.
5. Explain MouseMotionListener methods with example.
6. Write a Java program to demonstrate MouseListener interface.
7. Write a Java program to demonstrate MouseMotionListener interface.
8. Explain working of event handling in Java.
9. Explain advantages and disadvantages of Delegation Event Model.
10. Explain mouse events in Java GUI programming
9. References:
• [Link]
• [Link]
Srinivas Institute of Technology 13
Delegation Event Model-Handling Mouse Event
Srinivas Institute of Technology 14