What are the differences between a MouseListener and a MouseMotionListener in Java?



In this article, we will learn about the differences between a MouseListener and a MouseMotionListener in Java. We can implement a MouseListener interface when the mouse is stable while handling the mouse event, whereas we can implement a MouseMotionListener interface when the mouse is in motion while handling the mouse event.

Mouse Listener

A MouseListener is fired when we press, release or click (press followed by release) a mouse button (left or right button) at the source object or position the mouse pointer at (enter) and away (exit) from the source object.

Abstract Methods

A MouseListener interface declares the following five abstract methods:

mouseClicked(MouseEvent e)

Invoked when the mouse button has been clicked (pressed and released) on a component.

Method declaration:

public void mouseClicked(MouseEvent evt)

mousePressed(MouseEvent e)

Invoked when a component has pressed a mouse button.

Method declaration:

public void mousePressed(MouseEvent evt)

mouseReleased(MouseEvent e)

Invoked when a mouse button has been released on a component.

Method declaration:

public void mouseReleased(MouseEvent evt)

mouseEntered(MouseEvent e)

Invoked when the mouse enters a component.

Method declaration:

public void mouseEntered(MouseEvent evt)

mouseExited(MouseEvent e)

Invoked when the mouse exits a component.

Method declaration:

public void mouseExited(MouseEvent evt)

Use Cases for MouseListener

The following are some of the use cases for MouseListener in Java:

  • Button Clicks: Helpful in handling button clicks within applications, where you want to carry out an operation when the button is clicked.
  • Menu Choices: Useful for context menus or dropdown menus when entering or exiting results in visual changes.
  • Hover Effects: These will alter the appearance of a component when the mouse hovers over or exits it.

Example of MouseListener

Below is an example of MouseListener in Java to show pointer coordinates in the x-y planes:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MouseListenerTest extends JFrame implements MouseListener {
   private JTextField tfMouseX, tfMouseY;
   public MouseListenerTest() {
      setLayout(new FlowLayout());
      add(new Label("X-Click: "));
      tfMouseX = new JTextField(10);
      tfMouseX.setEditable(false);
      add(tfMouseX);
      add(new Label("Y-Click: "));
      tfMouseY = new JTextField(10);
      tfMouseY.setEditable(false);
      add(tfMouseY);
      addMouseListener(this);
      setTitle("MouseListener Test");
      setLocationRelativeTo(null);
      setSize(350, 100);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setVisible(true);
   }
   public static void main(String[] args) {
      new MouseListenerTest();
   }
   @Override
   public void mouseClicked(MouseEvent evt) {
      tfMouseX.setText(evt.getX() + "");
      tfMouseY.setText(evt.getY() + "");
   }
   // Need to provide an empty body to compile.
   @Override public void mousePressed(MouseEvent evt) { }
   @Override public void mouseReleased(MouseEvent evt) { }
   @Override public void mouseEntered(MouseEvent evt) { }
   @Override public void mouseExited(MouseEvent evt) { }
}

Output

MouseMotionListener

A MouseEvent is also fired when we move and drag the mouse pointer at the source object. But we need to use MouseMotionListener to handle the mouse-move and mouse-drag.

Abstract Methods

A MouseMotionListener interface declares the following two abstract methods:

mouseDragged(MouseEvent e)

Invoked when the mouse button is pressed on a component and then dragged. This method is repeated as the mouse moves while the button is held down.

Method declaration:

public void mouseDragged(MouseEvent e)

mouseMoved(MouseEvent e)

Invoked when the mouse cursor has been moved onto a component but no buttons have been pressed.

Method declaration:

public void mouseMoved(MouseEvent e)

Use Cases for MouseMotionListener

The following are some of the use cases for MouseMotionListener in Java:

  • Drawing Applications: Used for applications in which users generate drawings or paintings by manipulating the mouse.
  • Drag and Drop: Necessary for drag-and-drop operations where the position of the mouse during dragging matters.
  • Tooltips: Suitable for displaying small messages that track the mouse pointer as it moves over various regions of an element.

Example of MouseMotionListener

Below is an example of MouseMotionListener in Java to show pointer coordinates in the x-y planes:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MouseMotionListenerTest extends JFrame implements MouseListener, MouseMotionListener {
   private JTextField tfMouseClickX, tfMouseClickY;
   private JTextField tfMousePositionX, tfMousePositionY;
   public MouseMotionListenerTest() {
      setLayout(new FlowLayout());
      add(new Label("X-Click: "));
      tfMouseClickX = new JTextField(10);
      tfMouseClickX.setEditable(false);
      add(tfMouseClickX);
      add(new Label("Y-Click: "));
      tfMouseClickY = new JTextField(10);
      tfMouseClickY.setEditable(false);
      add(tfMouseClickY);
      add(new JLabel("X-Position: "));
      tfMousePositionX = new JTextField(10);
      tfMousePositionX.setEditable(false);
      add(tfMousePositionX);
      add(new JLabel("Y-Position: "));
      tfMousePositionY = new JTextField(10);
      tfMousePositionY.setEditable(false);
      add(tfMousePositionY);
      addMouseListener(this);
      addMouseMotionListener(this);
      setTitle("MouseMotionListener Test");
      setSize(400, 120);
      setLocationRelativeTo(null);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setVisible(true);
   }
   public static void main(String[] args) {
      new MouseMotionListenerTest();
   }
   @Override
   public void mouseClicked(MouseEvent evt) {
      tfMouseClickX.setText(evt.getX() + "");
      tfMouseClickY.setText(evt.getY() + "");
   }
   // Need to provide an empty body for compilation
   @Override public void mousePressed(MouseEvent evt) { }
   @Override public void mouseReleased(MouseEvent evt) { }
   @Override public void mouseEntered(MouseEvent evt) { }
   @Override public void mouseExited(MouseEvent evt) { }
   @Override
   public void mouseMoved(MouseEvent evt) {
      tfMousePositionX.setText(evt.getX() + "");
      tfMousePositionY.setText(evt.getY() + "");
   }
   // Need to provide an empty body to compile
   @Override public void mouseDragged(MouseEvent evt) { }
}

Output

Difference Table

The following are some key differences between a MouseListener and a MouseMotionListener in Java:

Criteria MouseListener MouseMotionListener
Primary Purpose Handle button actions Track movement
Event Types Clicks, presses, enters/exits Movement, dragging
Number of Methods 5 2
Performance Less frequent events More frequent events
Common Uses UI interactions Drawing, dragging, tracking
Alshifa Hasnain
Alshifa Hasnain

Converting Code to Clarity

Updated on: 2025-04-21T19:28:02+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements