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

AWTJavaObject Oriented ProgrammingProgrammingSwing

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 MouseEvent 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.

A MouseListener interface declares the following five abstract methods

Syntax

public void mouseClicked(MouseEvent evt)
public void mousePressed(MouseEvent evt)
public void mouseReleased(MouseEvent evt)
public void mouseEntered(MouseEvent evt)
public void mouseExited(MouseEvent evt)

Example

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.

A MouseMotionListener interface declares the following two abstract methods

Syntax

public void mouseDragged(MouseEvent e)
public void mouseMoved(MouseEvent e)

Example

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

raja
Published on 08-Jul-2019 07:58:33
Advertisements