Updated March 20, 2023
Introduction to Java MouseListener
In Java, MouseListener is a class that gets notified when there is a change in the mouse state. Changes of the mouse can be pressing, clicking, and releasing it. It can also be entering or exiting the window area. Mouselistener is working with the help of keyword implements and this listener interface can be gained from the java.awt.event package. More details on it will be explained in the following sections.
Declaration: Java MouseListener interface can be declared using the following syntax.
Public interface MouseListener extends EventListener
Methods of Java MouseListener
There are five methods. They are :
1. mouseClicked(MouseEventev): This method will get invoked when a Mouse button is clicked on a component.
2. mouseEntered(MouseEventev): This method will get invoked when a Mouse is entering a component.
3. mouseExited(MouseEventev): This method will get invoked when a Mouse is exiting a component.
4. mousePressed(MouseEventev): This method will get invoked when a Mouse button is pressed on a component.
5. mouseReleased(MouseEventev): This method will get invoked when a Mouse button is released on a component.
How Java MouseListener works?
- When the mouse is clicked by a person or a user, an object should be present in the program that helps in implementing the interface.
- Since the object is already registered with the listener, an event will be generated on clicking, pressing, entering, leaving or releasing the mouse.
- This will help in the invocation of the relevant method.
- After the invocation, the Mouse event is passed to it.
Examples
Below are the different examples:
Example #1
Java program to implement MouseListener Interface
Code:
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
//new class that implements MouseListener interface
public class JMouseListenerExample implements MouseListener
{
//create two labels lbl1 and lbl2
Label lbl1, lbl2;
//create a frame fr
Frame fr;
//create a sing s
String s;
JMouseListenerExample()
{
fr = new Frame("Java Mouse Listener Example");
lbl1= new Label("Demo for the mouse event", Label.CENTER);
lbl2= new Label();
//set the layout of frame as flowlayout
fr.setLayout(new FlowLayout());
//add label 1 to frame
fr.add(lbl1);
//add label 2 to frame
fr.add(lbl2);
//In order to catch as evll as respond to the mouse events, register the created class JMouseListenerExample
fr.addMouseListener(this);
//set the size of frame where width is 350 and height is 220
fr.setSize(350,220);
//set the visibility of frame as true
fr.setVisible(true);
}
//implementation of mouseClicked method
public void mouseClicked(MouseEvent ev)
{
s+=" then, the mouse button is clicked";
//set the text of label 2 as s
lbl2.setText(s);
//set the visibility as true
fr.setVisible(true);
}
//implementation of mouseEntered method
public void mouseEntered(MouseEvent ev)
{
//set the text of label 2 when mouse enters the window
lbl2.setText("Now, the mouse has entered the area of window");
//set the visibility as true
fr.setVisible(true);
}
//implementation of mouseExited method
public void mouseExited(MouseEvent ev)
{
//set the text of label 2 when the mouse leaves the window
lbl2.setText("Mouse has left the area of window");
//set the visibility as true
fr.setVisible(true);
}
//implementation of mousePressed method
public void mousePressed(MouseEvent ev)
{
//set the text of label 2 when the mouse is pressed
lbl2.setText("Mouse button is being pressed");
//set the visibility as true
fr.setVisible(true);
}
//implementation of mouseReleased method
public void mouseReleased(MouseEvent ev)
{
//set the string s
s="Firstly, the mouse button is released and ";
//set the text of label 2
lbl2.setText(s);
//set the visibility as true
fr.setVisible(true);
}
//main method
public static void main(String args[])
{
new JMouseListenerExample();
}
}
Output:
An output as shown below will be displayed on executing the code.
When the mouse enters the window, a label showing a mouse entered the window area will be displayed.
Similarly, when the mouse leaves the window, the following label will be shown.
Moreover, on pressing the mouse, a text will be displayed on the window as depicted below.
After releasing the pressed button and clicking again, the label shows that the mouse is released and it is then clicked.
Example #2
Java program to implement MouseListener and display colors on mouse clicking event
Code:
import java.awt.Color;
import java.awt.Frame;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
//class that implements MouseListener interface
public class JMouseListenerExample extends Frame implements MouseListener
{
public JMouseListenerExample( )
{
//method for frame linking
addMouseListener(this);
//set the size where width and height is 350
setSize(350,350);
//set the visibility as true
setVisible(true);
}
//implementation of mousePressed method
public void mousePressed(MouseEvent ev)
{
//on pressing the mouse, background color will be green
setBackground(Color.green);
//text to print
System.out.println("Pressing Mouse");
}
//implementation of mouseReleased method
public void mouseReleased(MouseEvent ev)
{
//on pressing the mouse, background color will be pink
setBackground(Color.pink);
//text to print
System.out.println("Released the mouse");
}
//implementation of mouseClicked method
public void mouseClicked(MouseEvent ev)
{
//on clicking the mouse, background color will be yellow
setBackground(Color.yellow);
//text to print
System.out.println("Clicked");
}
//implementation of mouseEntered method
public void mouseEntered(MouseEvent ev)
{
//on entering the window, background color will be red
setBackground(Color.red);
//text to print
System.out.println("Entered the window");
}
//implementation of mouseExited method
public void mouseExited(MouseEvent ev)
{
//on exiting the window, background color will be white
setBackground(Color.white);
//text to print
System.out.println("Mouse left the window");
}
//main method
public static void main(String args[])
{
new JMouseListenerExample();
}
}
Output:
In this program, background colors will be changed on different mouse events and at the same time, a text will be displayed on the console.
Firstly, on entering the window, the background color will be red.
Similarly on leaving the window, color changes to white.
When the mouse is pressed, the green color will be displayed.
On releasing the mouse and clicking it, a background color yellow will be shown.
However, if the mouse is released and not clicked, pink will be displayed instead of yellow color as shown below.
Conclusion
It is a class that gets notified when certain mouse events happen. More details such as declaration, methods, and implementation of Java MouseListener is discussed in this document in detail.
Recommended Article
This is a guide to Java MouseListener. Here we discuss the introduction to MouseListener along with the respective examples to implement Java MouseListener. You can also go through our other suggested articles to learn more –