MCS024 : Question 2 (b) Mouse Motion Listener Implementation in Java | June 2023 Paper Solution

mcs024 june 2023 Paper

Question 2. (b) Briefly discuss the concept of listener in Java. Write a program in java to implement mouse motion listener, support your program with suitable comments.


Listener in Java:
In Java, a listener is an interface that allows objects to respond to events or changes in the program. Listeners are commonly used in graphical user interfaces (GUIs) to handle user interactions, such as button clicks, mouse movements, and keyboard inputs. They enable event-driven programming, where specific actions are triggered by user interactions.

Mouse Motion Listener Program:

Here's a Java program that demonstrates the implementation of a MouseMotionListener interface. This interface allows us to track and respond to mouse movement events. The program captures the mouse's X and Y coordinates as it moves and displays them on the console.

import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.JFrame;

public class MouseMotionListenerExample implements MouseMotionListener {

public static void main(String[] args) {
// Create a JFrame window
JFrame frame = new JFrame("Mouse Motion Listener Example");
frame.setSize(300, 200);

// Add mouse motion listener to the frame
frame.addMouseMotionListener(new MouseMotionListenerExample());


// Mouse motion listener methods
public void mouseDragged(MouseEvent e) {
// Not used in this example

public void mouseMoved(MouseEvent e) {
int x = e.getX(); // Get the X coordinate of the mouse
int y = e.getY(); // Get the Y coordinate of the mouse

System.out.println("Mouse moved. X: " + x + ", Y: " + y);


  1. We start by creating a JFrame window, setting its size, and defining the close operation.
  2. Next, we create an instance of the MouseMotionListenerExample class and add it as a mouse motion listener to the JFrame using the addMouseMotionListener() method. This ensures that the JFrame listens to mouse motion events.
  3. The MouseMotionListenerExample class implements the MouseMotionListener interface, which requires overriding two methods: mouseDragged() and mouseMoved().
  4. In our example, we utilize the mouseMoved() method to capture the X and Y coordinates of the mouse as it moves within the JFrame. These coordinates are obtained using the getX() and getY() methods from the MouseEvent object passed as a parameter.
  5. We display the mouse coordinates by printing them on the console using System.out.println().


By implementing the MouseMotionListener interface, we can track and respond to mouse movement events in Java. In this program, we demonstrated the implementation of a MouseMotionListener to capture and display the X and Y coordinates of the mouse as it moves within a JFrame window. This example showcases the basic usage of the MouseMotionListener and can be expanded to include additional functionalities based on different mouse movement scenarios.

Post a Comment