Friday 7 September 2018

Java Swing Mouse Motion Listener With Program in Java | MouseMotionListener Interface and Methods

                      In this post, we will see Java Swing Mouse Motion Listener With Program in Java | MouseMotionListener Interface and Methods.
                      In previous post, we have seen use of Java Swing MouseListener interface. Check here

Watch following video:

How to use MouseMotionListener interface?
                    Use of MouseMotionListener interface in Java Swing is similar to MouseListener inteface. Only the difference is in the methods used.
                    To generate event, we have to register component (button or label or textfield etc.) for event by calling method addMouseMotionListener(). This method accepts object of MouseMotionListener interface. As we can not create object of interface, we have to implement MouseMotionListener interface into class and we need to pass object of this class to addMouseMotionListener() method as argument. this represents the current object of class.
                    addMouseMotionListener() implicitly call methods mouseDragged(), mouseMoved() depending on mouse action. These methods accepts object of MouseEvent class as argument. These methods are declared in MouseMotionListener interface. Whatever the action we have to take for event, we have to mention in these methods.
                    Go through the following program:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class SwingMouseMotionListener
   public static void main(String args[])
       Abc obj=new Abc();

class Abc extends JFrame implements MouseMotionListener
  JLabel l1,l2,l3;
  public Abc()

     setLayout(new FlowLayout());

     l1=new JLabel("Mouse Action");
     l2=new JLabel("Mouse Cursor Position:");
     l3=new JLabel("Take Mouse Cursor Here");



 public void mouseDragged(MouseEvent ae)
   int x=ae.getX();
   int y=ae.getY();
   l1.setText("Mouse is Dragged");
   l2.setText("Cursor Position: "+x+","+y); 
 public void mouseMoved(MouseEvent ae)
   int x=ae.getX();
   int y=ae.getY();
   l1.setText("Mouse is Moved");
   l2.setText("Cursor Position: "+x+","+y); 


How To Run:
parag@parag-Inspiron-N4010:~/Desktop/prog$ javac 
parag@parag-Inspiron-N4010:~/Desktop/prog$ java SwingMouseMotionListener

                     When Mouse Cursor is Dragged over Label "Take Mouse Cursor Here" (i.e. Mouse button is pressed and cursor is moved):

                     When Mouse Cursor is moved over Label "Take Mouse Cursor Here" (without pressing Mouse button):

                             To know what is Swing, how Swing is different from Applet, how to create Swing Application; check this post                    

                    To know how to generate event in Java Swing along with Java Swing program for addition of two numbers; check this post 

                    Find Java Swing program for Calculator in this post

                     Find Java Swing MouseListener Example 

 in this post

                        Check other posts on Java Programming in this link

No comments:

Post a Comment