by Dinesh Thakur

The addMouseMotionListener () method to handle events related to mouse movements. methods mouseDragged () and mouseMoved () receive events. Any of the objects of the Canvas, JDialog, JFrame, JPanel and JWindow classes can produce such events. To mark an object can listen to these events must implement the interface MouseMotionListener, and we have added the producer of events by the method:

public void addMouseMotionListener(MouseMotionListener mml)

import java.applet.Applet;

import java.awt.*;
import java.awt.event.*;
/* <APPLET CODE ="MouseMotionListenerExample.class" WIDTH=300 HEIGHT=200> </APPLET> */
public class MouseMotionListenerExample extends Applet implements MouseListener,MouseMotionListener
    {
         TextField t;
         public void init()
             {
                 t=new TextField(100);
                 add(t);
                 addMouseListener(this);
                 addMouseMotionListener(this);
             }
                 public void mousePressed(MouseEvent e)
                    {
                        t.setText("Mouse Pressed; # of clicks: "+e.getClickCount()+"at position "+e.getX()+","+e.getY());
                    }
                      public void mouseReleased(MouseEvent e)
                          {
                              t.setText("Mouse released; #of clicks: " +e.getClickCount());
                          }
                              public void mouseEntered(MouseEvent e)
                                  {
                                     t.setText("Mouse Entered");
                                  }
                                     public void mouseExited(MouseEvent e)
                                       {
                                          t.setText("Mouse Exited");
                                       }
                                          public void mouseClicked(MouseEvent e)
                                             {
                                                t.setText("Mouse clicked(# of clicks:"+e.getClickCount());
                                             }
                                                 public void mouseMoved(MouseEvent e)
                                                   {
                                                       t.setText("Mouse is moved to Location"+e.getX()+","+e.getY());
                                                    }
                                                        public void mouseDragged(MouseEvent e)
                                                           {
                                                              t.setText("Mouse is dragged to Location:"+e.getX()+","+e.getY());
                                                          }
                                                                      
                                                 
     }            

MouseMotionListener Example in Java