Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
We all have seen interactive applications that perform some action whenever the mouse is not in motion. But how do internal things work? How to implement them in Java? To answer all these questions, we have Java MouseListener. Java MouseListener is an interface in the java.awt.package, and it is useful in interacting with the user whenever the mouse is not in motion.
In this article, we will learn about Java MouseListener, its declaration, and its methods with detailed examples. So let’s get started with our article.
Java MouseListener is an interface in java.awt.event package. MouseListener operates the events when the mouse is not in motion.Java MouseListener is alerted whenever we change the state of the mouse. Java MouseListener has five methods.
Java MouseListener interface can be declared by the following syntax:
public interface MouseListener extends EventListener
You can also try this code with Online Java Compiler
Whenever a mouse is clicked, an object must be present in the program which helps in implementing the interface. As the object is defined by the listener, an event will be generated by pressing, clicking, leaving, entering, or releasing the mouse. For this, we need different methods. Following are the various methods that are present in Java MouseListener:
Method
Description
Void mouseReleased(MouseEvent e)
Mouse key is released
Void mouseExited(MouseEvent e)
Mouse exited the component
Void mouseEnetered(MouseEvent e)
Mouse entered the component
Void mousePressed(MouseEvent e)
Mouse key is pressed
Void mouseClicked(MouseEvent e)
Mouse key is released / pressedThe mouse
Example
This section will discuss the example which handles Java MouseListener.
Code 1:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class MyMouse extends JFrame implements MouseListener
{
JLabel label;
MyMouse(){
addMouseListener(this);
label = new JLabel();
label.setBounds(90,80,130,20);
label.setFont(new Font("Serif", Font.BOLD, 20));
add(label);
setSize(250,250);
setLayout(null);
setVisible(true);
}
public void mouseClicked(MouseEvent e) {
label.setText("Clicked");
}
public void mouseEntered(MouseEvent e) {
label.setText("Entered");
}
public void mouseExited(MouseEvent e) {
label.setText("Exited");
}
public void mousePressed(MouseEvent e) {
label.setText("Pressed");
}
public void mouseReleased(MouseEvent e) {
label.setText("Released");
}
public static void main(String[] args) {
new MyMouse();
}
}
You can also try this code with Online Java Compiler
In the above example, as we can see we have used a class called MyMouse in which we have created different methods to handle mouselistener. Below is the output generated using the above code.
This is the output generated when we exited the mouse from the output frame.
And, This is the output generated when we have clicked the mouse on the output frame.
In the above example, we can see that we have created a class called NInja in which we have created different methods to display the action on the output screen. Using these methods we have created the above output. Whenever the mouse is actioned on the screen it will display according to the action in the output.
The purpose of Java MouseListener is to notify us, whenever we change the state of the mouse.
What is the difference between Java MouseListener and Java MotionListener?
Java MotionListener Interface is used when the mouse is in stable mode whereas the Java MouseListener interface is used when the mouse is in motion while handling the mouse event.
How many methods are present in Java MouseListener?
There are five methods present in Java MouseListener : mousePressed(MouseEvent e); mouseReleased(MouseEvent e); mouseClicked(MouseEvent e); mouseEntered(MouseEvent e); mouseExited(MouseEvent e);
What is the difference between Java MouseAdaptor and Java MouseListener?
Java MouseListener is an interface whereas Java MouseAdaptor is an implementation of it. We can use Java MouseAdaptor in every place where you can use Java MouseListener.
Which command does not generate any events?
The command known as TextArea objects does not generate any events.
Conclusion
This article extensively discussed Java MouseListener and its different methods. We started with an Introduction to Java MouseListener, then saw some of the methods of Java MouseListenert, and after that, we have seen some examples of handling Java MouseListener.