Thursday, 6 September 2018

Java Swing MouseMotionListener Example

                      In this post, we will see how to handle events related to mouse actions (dragged, moved) in Java Swing by using MouseMotionListener interface.

                      In previous post, we have seen use of Java Swing MouseListener interface. Check here https://www.comrevo.com/2017/08/java-swing-mouselistener-example.html

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:
                                
SwingMouseMotionListener.java 

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");
          
     add(l1);
     add(l2);
     add(l3);
     
     l3.addMouseMotionListener(this);
          
     setVisible(true);
     setSize(250,300);

     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  }

 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 SwingMouseMotionListener.java 
parag@parag-Inspiron-N4010:~/Desktop/prog$ java SwingMouseMotionListener

Output:
                     
                     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 http://www.comrevo.com/2017/08/swing-in-java-with-example.html                    

                    To know how to generate event in Java Swing along with Java Swing program for addition of two numbers; check this post http://www.comrevo.com/2017/08/java-swing-program-for-addition-of-two-numbers.html 

                    Find Java Swing program for Calculator in this post http://www.comrevo.com/2017/08/java-swing-calculator-program.html

                     Find Java Swing MouseListener Example 

 in this post https://www.comrevo.com/2017/08/java-swing-mouselistener-example.html

                        Check other posts on Java Programming in this link http://www.comrevo.com/2017/08/java-programming.html


No comments:

Post a comment