The listener is referred to as the instance of the class in Java that has a responsibility to process certain kinds of events specified to it. The listener performs the action for a specific event. When the user performs a certain action, in response to that specific action, the listener activates and performs the task. Some of the most common types of listeners are Action Listeners, Key Listeners, Window Listeners, and Mouse Listeners.
This write-up will discuss in detail the working of MouseListeners in Java.
How to Use/Employ Mouse Listeners in Java?
The mouse listener in Java responds to certain events when a user performs specific tasks through the mouse. The listener interface activates itself and responds according to the user’s needs. Whenever a change of state occurs the Mouse Listener in Java activates itself and works accordingly.
There are five Mouse Listeners in Java depicted below:
- public abstract void Clicked(MouseEvent m);
- public abstract void Entered(MouseEvent m);
- public abstract void Exited(MouseEvent m);
- public abstract void Pressed(MouseEvent m);
- public abstract void Released(MouseEvent m);
Example 1: Code Implementation for Mouse Listeners in Java
The below code depicts the implementation of five Mouse Listeners in Java.
//Import the action listener packages in Java
import java.awt.*;
import java.awt.event.*;
//Create a class for mouse listener that implements the
class listenerex extends Frame implements MouseListener {
Label x;
listenerex() {
addMouseListener(this);
setTitle("Welcome to Mouse Action Listener in Java");
// Declare the label and set the parameters for the frame
x = new Label();
x.setBounds(50, 50, 150, 150);
add(x);
// Set the size of the frame.
setSize(300, 300);
setLayout(null);
// Set the visibility as true.
setVisible(true);
}
// The built-in method invokes itself when the mouse has been clicked.
public void mouseClicked(MouseEvent m) {
x.setText("Clicked");
}
// This built-in method invokes when the mouse has entered a component.
public void mouseEntered(MouseEvent m) {
x.setText("Entered");
}
// The built-in method when the mouse has exited a certain component.
public void mouseExited(MouseEvent m) {
x.setText("Exit");
}
// The built-in method when the mouse has been pressed.
public void mousePressed(MouseEvent m) {
x.setText("Pressed");
}
// The built-in method when the mouse has been released.
public void mouseReleased(MouseEvent e) {
x.setText("Released");
}
// The main class to execute the object.
public static void main(String[] args) {
new listenerex();
}
}
In the above code:
- To create a frame the Action Listener packages are imported.
- The required parameters for the frame are declared.
- The Clicked method invokes itself when the mouse is clicked.
- The Entered method invokes itself when the mouse has entered the frame.
- The Exit method invokes itself when the mouse exits the frame.
- The Pressed method invokes itself when the mouse is pressed.
- The Released method invokes itself when the mouse is released.
Output
The below output shows the results when the mouse gets clicked, pressed, entered, released, and exits the current frame.

This sums up the implementation of Mouse Listeners in Java.
Conclusion
The Mouse listeners in Java perform specific tasks. The mouse listener activates itself to perform specific tasks through the mouse. The different mouse listeners in Java work when the mouse action like clicked, entered, pressed, released or exit occurs. This article has effectively demonstrated the working of Mouse listeners in Java in detail.