Wednesday 23 August 2017

Java Swing Mouse Listener | MouseListener in Java Swing Example | MouseListener Interface in Java

                      In this post, we will see Java Swing Mouse Listener | MouseListener in Java Swing Example | MouseListener Interface in Java.

Watch following video:


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

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

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

class Abc extends JFrame implements MouseListener
{
  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.addMouseListener(this);
          
     setVisible(true);
     setSize(250,300);

     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  }

 public void mousePressed(MouseEvent ae)
  {
   int x=ae.getX();
   int y=ae.getY();
   l1.setText("Mouse is Pressed");
   l2.setText("Cursor Position: "+x+","+y); 
  }
 public void mouseReleased(MouseEvent ae)
  {
   int x=ae.getX();
   int y=ae.getY();
   l1.setText("Mouse is Released");
   l2.setText("Cursor Position: "+x+","+y); 
  }
 public void mouseClicked(MouseEvent ae)
  {
   int x=ae.getX();
   int y=ae.getY();
   l1.setText("Mouse is Clicked");
   l2.setText("Cursor Position: "+x+","+y); 
  }
 public void mouseEntered(MouseEvent ae)
  {  
   int x=ae.getX();
   int y=ae.getY();
   l1.setText("Mouse is Entered");
   l2.setText("Cursor Position: "+x+","+y); 
  }
 public void mouseExited(MouseEvent ae)
  {
   int x=ae.getX();
   int y=ae.getY();
   l1.setText("Mouse is Exited");
   l2.setText("Cursor Position: "+x+","+y); 
  }

}
  


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

Output:
                     
                     When Mouse Cursor is taken to Label "Take Mouse Cursor Here":

                     When Mouse Cursor is exited from Label "Take Mouse Cursor Here":

                     When Mouse Cursor is pressed on Label "Take Mouse Cursor Here":

                     When Mouse Cursor is Clicked (i.e. Pressed & Released) on Label "Take Mouse Cursor Here":


                    Check Java Swing MouseMotionListener Example in following post:
https://www.comrevo.com/2018/09/java-swing-mouse-motion-listener-example.html
 



                    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

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


No comments:

Post a Comment