Introduction to Mouse Listener Class in Java

This article explains the Mouse Listener class in Java.

Introduction

 
This article explains the Mouse Listener class in Java.
 

What the Mouse Listener is?

 
When a user interacts with a component using the mouse an event is generated called a mouse event since it is generated by the mouse. It is generated when we are working with the mouse, like clicking somewhere or releasing or pressing the mouse, etcetera. For tracking the mouse cursor Java provides another separate interface called MOuseMotionListener. For tracking the wheel position of the mouse Java provides MouseWheel Listener.
 

Mouse Event Methods

 
There are five public methods in this class that are:
  • void mouseClicked(MouseEvent mevnt)
          It is called when the user clicks somewhere in the window.
  • void mouseEntered(MouseEvent mevnt)
          It is called when the cursor is inside the region of the window.
  • void mouseExited(MouseEvent mevnt)
          It is called when the cursor leaves the region of the window.
  • void mousePressed(MouseEvent mevnt)
          It is called when the user clicks or presses a mouse button.
  • void mouseReleased(MouseEvent mevnt)
          It is called when the user releases a mouse button.
 
Examples
 
1.MouseEventEx1.java
 
In this example; we create a Java class that shows the MouseEvent functionality, such as it shows the location of the mouse when clicked, moved, dragged and when the mouse is moved outside it shows that the mouse has gone outside of the window. In this class we implement two listeners, MouseMotionListener and MouseListener. MouseMotionListener shows the location of a movable mouse pointer and the other interface shows the other events of the mouse.
  1. import javax.swing.*;  
  2. import java.awt.event.*;  
  3. import java.awt.*;  
  4. public class MouseEventEx1 extends JFrame implements MouseMotionListener, MouseListener   
  5. {  
  6.  private JLabel jlabl;  
  7.  public MouseEventEx1()   
  8.  {  
  9.   super("Mouse Tracker In Java");  
  10.   jlabl = new JLabel();  
  11.   jlabl.setBackground(Color.gray);  
  12.   jlabl.setForeground(Color.blue);  
  13.   jlabl.setFont(new Font("Segoe UI", Font.BOLD, 18));  
  14.   getContentPane().add(jlabl, BorderLayout.SOUTH);  
  15.   setSize(600350);  
  16.   setLocation(250250);  
  17.   addMouseMotionListener(this);  
  18.   addMouseListener(this);  
  19.   show();  
  20.  }  
  21.  public void mouseClicked(MouseEvent menvt)  
  22.  {  
  23.   jlabl.setText("Clicked at following location [" + menvt.getX() + ", " + menvt.getY() + "]");  
  24.  }  
  25.  public void mousePressed(MouseEvent menvt)   
  26.  {  
  27.   jlabl.setText("Pressed at following location [" + menvt.getX() + ", " + menvt.getY() + "]");  
  28.  }  
  29.  public void mouseReleased(MouseEvent menvt)   
  30.  {  
  31.   jlabl.setText("Released at following location [" + menvt.getX() + ", " + menvt.getY() + "]");  
  32.  }  
  33.  public void mouseEntered(MouseEvent menvt)   
  34.  {  
  35.   jlabl.setText("Mouse still inside window");  
  36.  }  
  37.  public void mouseExited(MouseEvent menvt)   
  38.  {  
  39.   jlabl.setText("Mouse went outside window");  
  40.  }  
  41.  public void mouseDragged(MouseEvent menvt)   
  42.  {  
  43.   jlabl.setText("Dragged at following location [" + menvt.getX() + ", " + menvt.getY() + "]");  
  44.  }  
  45.  public void mouseMoved(MouseEvent menvt)  
  46.  {  
  47.   jlabl.setText("Moved at following location [" + menvt.getX() + ", " + menvt.getY() + "]");  
  48.  }  
  49.  public static void main(String args[])   
  50.  {  
  51.   MouseEventEx1 applcn = new MouseEventEx1();  
  52.   applcn.addWindowListener(new WindowAdapter()   
  53.   {  
  54.    public void windowClosing(WindowEvent menvt)   
  55.    {  
  56.     System.exit(0);  
  57.    }  
  58.   });  
  59.  }  
  60. }   
Output
Fig-1.jpg
 
When we press Enter we get a new window and the following figure shows the location of the movable mouse's current position.
 
fig-2.jpg
 
When we press the mouse somewhere in a new window it shows the location as in the following. Similarly, we can do another event also, like mouse dragging.
 
Fig-3.jpg
 
2. MouseEventEx2.java
 
In this example; we create a frame for the purpose of drawing or painting. In this example, we use a Swing component like graphics to design or to provide a way to paint something on the frame.
  1. import java.awt.event.*;  
  2. import javax.swing.*;  
  3. import java.awt.*;  
  4. public class MouseEventEx2 extends JFrame   
  5. {  
  6.  private int xVal = -10, yVal = -10;  
  7.  Public MouseEventEx2()  
  8.  {  
  9.   super("Painting In Java");  
  10.   getContentPane().add(new Label("Starts your painting by dragging mouse"), BorderLayout.NORTH);  
  11.   addMouseMotionListener  
  12.    (new MouseMotionAdapter()  
  13.    {  
  14.     public void mouseDragged(MouseEvent mevnt) {  
  15.      xVal = mevnt.getX();  
  16.      yVal = mevnt.getY();  
  17.      repaint();  
  18.     }  
  19.    });  
  20.   setSize(630350);  
  21.   setLocation(350350);  
  22.   show();  
  23.  }  
  24.  public void paint(Graphics grp)   
  25.  {  
  26.   grp.fillOval(xVal, yVal, 55);  
  27.  }  
  28.  public static void main(String args[])   
  29.  {  
  30.   MouseEventEx2 applcn = new MouseEventEx2();  
  31.   applcn.addWindowListener(new WindowAdapter() {  
  32.    public void windowClosing(WindowEvent mevnt) {  
  33.     System.exit(0);  
  34.    }  
  35.   });  
  36.  }  
  37. }  
Output
Fig-4.jpg
 
After pressing enter we get a new frame where we can draw anything using the mouse.
 
fig-5.jpg
 
The following figure shows a demonstration of painting in Java using the WindowEvent class.
 
fig-6.jpg
 
3. MouseEventEx3.java
 
In this example; we create a frame to show the perfect location of the mouse clicked and also shows which button you used, like left, right or center button.
  1. import java.awt.event.*;  
  2. import javax.swing.*;  
  3. import java.awt.*;  
  4. public class MouseEventEx3 extends JFrame  
  5. {  
  6.  private int xCord, yCord;  
  7.  private String str = "";  
  8.  public MouseEventEx3()   
  9.  {  
  10.   addMouseListener(new HandlingClick());  
  11.   setSize(600350);  
  12.   setLocation(350350);  
  13.   show();  
  14.  }  
  15.  public void paint(Graphics grp)   
  16.  {  
  17.   grp.drawString("You clicled @ [" + xCord + ", " + yCord + "] ", xCord, yCord);  
  18.  }  
  19.  public static void main(String args[])   
  20.  {  
  21.   MouseEventEx3 applcn = new MouseEventEx3();  
  22.   applcn.addWindowListener(new WindowAdapter()   
  23.   {  
  24.    public void windowClosing(WindowEvent mevnt)  
  25.    {  
  26.     System.exit(0);  
  27.    }  
  28.   });  
  29.  }  
  30.  private class HandlingClick extends MouseAdapter   
  31.  {  
  32.   public void mouseClicked(MouseEvent mevnt)   
  33.   {  
  34.    xCord = mevnt.getX();  
  35.    yCord = mevnt.getY();  
  36.    String str = "You clicked " + mevnt.getClickCount();  
  37.    str += " time(str)";  
  38.    if (mevnt.isMetaDown())  
  39.     str += " using right button of mouse";  
  40.    else if (mevnt.isAltDown())  
  41.     str += " using center button of mouse";  
  42.    else  
  43.     str += " using left button of mouse";  
  44.    setTitle(str);  
  45.    repaint();  
  46.   }  
  47.  }  
  48. }  
Output
fig-7.jpg
 
After pressing enter we get a new frame which displays the coordinates of the mouse click.
 
fig-8.jpg
 
The following figure shows the clicking event as when first we click using the left button of mouse it shows coordinates (207, 140) and again we click the right button of the mouse and it shows on the title that you are clicking the right button and shows the coordinates (208,176).
 
fig-9.jpg