Mouse Event Handling

Learn tracking of mouse events

In order to handle mouse events, Mouse Listener has to be defined. Mouse listener is the handler which will handle mouse events. There are two types of mouse listener

1. MouseListener
This listener is used to track click,press, release, entered and exited events.

2. MouseMotionListener
This listener is used to track mouse movements, drag movements etc.


Methods of MouseListener Interface

Method Description

public void mouseClicked()

This method is called when mouse button will be clicked. Mouse click event actually composed of two event button pressing and releasing.

public void mousePressed() This method is called when mouse button is pressed.
public void mouseReleased() This method is called when a pressed mouse button is released.
public void mouseEntered() This method is called when mouse pointer moved in area of component for which listener is registered.
public void mouseExited() This method is called when mouse pointer is moved out from area of components for which listener is registered.

Example

First define mouse event handler by implementing MouseListener interface


import javax.swing.*;			
import java.awt.event.*;
class mouseHandler implements MouseListener
{
  public void mouseClicked(MouseEvent me)
   {
     System.out.println("Mouse Button is Clicked");
   }
  public void mousePressed(MouseEvent me)
   {
     System.out.println("Mouse Button is Pressed");
   }
public void mouseReleased(MouseEvent me)
   {
     System.out.println("Mouse Button is Released");
   }
public void mouseEntered(MouseEvent me)
   {
     System.out.println("Mouse Pointer Entered in Frame");
   }  
public void mouseExited(MouseEvent me)
   {
     System.out.println("Mouse Pointer is Exited");
   }   
}

In above code MouseListener interface is implemented in class mouseHandler.
Second we have to create a frame for which mouse listener will be registered for handling mouse event occuring on frame.

Create frame and attach listener with frame.


class fr
 {
   public static void main(String arg[])
    {
	  JFrame jf=new JFrame ("Mouse Event ");
	  jf.setSize(400,400);
	  jf.setVisible(true);
	  jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	  // Create object of listener class
	  
	  mouseHandler mh=new mouseHandler();
	  
	  // Registering Mouse listener for frame.
	  jf.addMouseListener(mh);
	  
	}  
 } 

Complete Program with two classes

This program is having two different classes. One class is implementing listener interface and in second class creating GUI.


import javax.swing.*;			
import java.awt.event.*;
class mouseHandler implements MouseListener
{
  public void mouseClicked(MouseEvent me)
   {
     System.out.println("Mouse Button is Clicked");
   }
  public void mousePressed(MouseEvent me)
   {
     System.out.println("Mouse Button is Pressed");
   }
public void mouseReleased(MouseEvent me)
   {
     System.out.println("Mouse Button is Released");
   }
public void mouseEntered(MouseEvent me)
   {
     System.out.println("Mouse Pointer Entered in Frame");
   }  
public void mouseExited(MouseEvent me)
   {
     System.out.println("Mouse Pointer is Exited");
   }   
}
class fr
 {
   public static void main(String arg[])
    {
	  JFrame jf=new JFrame ("Mouse Event ");
	  jf.setSize(400,400);
	  jf.setVisible(true);
	  jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	  // Create object of listener class
	  
	  mouseHandler mh=new mouseHandler();
	  
	  // Registering Mouse listener for frame.
	  jf.addMouseListener(mh);
	  
	}  
 } 

Complete Program with one class

In this program one class is used to implement litener interface and to create GUI.


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

class fr implements MouseListener
 {
   public static void main(String arg[])
    {
	  JFrame jf=new JFrame ("Mouse Event ");
	  jf.setSize(400,400);
	  jf.setVisible(true);
	  jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	  // Create object of listener class
	  
	  
	  
	  // Registering Mouse listener for frame.
	  jf.addMouseListener(new fr());
	  
	} 
  public void mouseClicked(MouseEvent me)
   {
     System.out.println("Mouse Button is Clicked");
   }
  public void mousePressed(MouseEvent me)
   {
     System.out.println("Mouse Button is Pressed");
   }
public void mouseReleased(MouseEvent me)
   {
     System.out.println("Mouse Button is Released");
   }
public void mouseEntered(MouseEvent me)
   {
     System.out.println("Mouse Pointer Entered in Frame");
   }  
public void mouseExited(MouseEvent me)
   {
     System.out.println("Mouse Pointer is Exited");
   } 	
 } 

Output