Introduction

The interfaceMouseMotionListener is used for receiving mouse motion events on a component. The class that process mouse motion events needs to implements this interface.

Class declaration

Following is the declaration for java.awt.event.MouseMotionListener interface:

  1. public interface MouseMotionListener
  2. extends EventListener

Interface methods

S.N.Method & Description
1 void mouseDragged(MouseEvent e) Invoked when a mouse button is pressed on a component and then dragged.
2 void mouseMoved(MouseEvent e) Invoked when the mouse cursor has been moved onto a component but no buttons have been pushed.

Methods inherited

This class inherits methods from the following interfaces:

  • java.awt.event.EventListener

MouseMotionListener Example

Create the following java program using any editor of your choice in say D:/ > AWT > com > tutorialspoint > gui > AwtListenerDemo.java

  1. package com.tutorialspoint.gui;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5.  
  6. public class AwtListenerDemo {
  7. private Frame mainFrame;
  8. private Label headerLabel;
  9. private Label statusLabel;
  10. private Panel controlPanel;
  11.  
  12. public AwtListenerDemo(){
  13. prepareGUI();
  14. }
  15.  
  16. public static void main(String[] args){
  17. AwtListenerDemo awtListenerDemo = new AwtListenerDemo();
  18. awtListenerDemo.showMouseMotionListenerDemo();
  19. }
  20.  
  21. private void prepareGUI(){
  22. mainFrame = new Frame("Java AWT Examples");
  23. mainFrame.setSize(400,400);
  24. mainFrame.setLayout(new GridLayout(3, 1));
  25. mainFrame.addWindowListener(new WindowAdapter() {
  26. public void windowClosing(WindowEvent windowEvent){
  27. System.exit(0);
  28. }
  29. });
  30.  
  31. headerLabel = new Label();
  32. headerLabel.setAlignment(Label.CENTER);
  33. statusLabel = new Label();
  34. statusLabel.setAlignment(Label.CENTER);
  35. statusLabel.setSize(350,100);
  36.  
  37. controlPanel = new Panel();
  38. controlPanel.setLayout(new FlowLayout());
  39.  
  40. mainFrame.add(headerLabel);
  41. mainFrame.add(controlPanel);
  42. mainFrame.add(statusLabel);
  43. mainFrame.setVisible(true);
  44. }
  45.  
  46. private void showMouseMotionListenerDemo(){
  47. headerLabel.setText("Listener in action: MouseMotionListener");
  48.  
  49. Panel panel = new Panel();
  50. panel.setBackground(Color.magenta);
  51. panel.setLayout(new FlowLayout());
  52. panel.addMouseMotionListener(new CustomMouseMotionListener());
  53.  
  54. Label msglabel = new Label();
  55. msglabel.setAlignment(Label.CENTER);
  56. msglabel.setText("Welcome to TutorialsPoint AWT Tutorial.");
  57. panel.add(msglabel);
  58.  
  59. controlPanel.add(panel);
  60.  
  61. mainFrame.setVisible(true);
  62. }
  63.  
  64. class CustomMouseMotionListener implements MouseMotionListener {
  65.  
  66. public void mouseDragged(MouseEvent e) {
  67. statusLabel.setText("Mouse Dragged: ("+e.getX()+", "+e.getY() +")");
  68. }
  69.  
  70. public void mouseMoved(MouseEvent e) {
  71. statusLabel.setText("Mouse Moved: ("+e.getX()+", "+e.getY() +")");
  72. }
  73. }
  74. }

Compile the program using command prompt. Go to D:/ > AWT and type the following command.

  1. D:\AWT>javac com\tutorialspoint\gui\AwtListenerDemo.java

If no error comes that means compilation is successful. Run the program using following command.

  1. D:\AWT>java com.tutorialspoint.gui.AwtListenerDemo

Verify the following output

awt_mousemotionlistener.jpg