You are here:   HomeJava ProgrammingAWT and AppletsWhat is Repainting method in java?
by Dinesh Thakur Category: AWT and Applets

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





Subscribe To Free Daily Newsletter!

Get Free News Updates Delivered Directly To Your Inbox
About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to contact us.



What's New and Popular





Popular Article