Table of contents
1.
Introduction
2.
Java MouseListener
3.
Methods of Java MouseListener
4.
Example
5.
Frequently Asked Questions
5.1.
What is the purpose of Java MouseListener?
5.2.
What is the difference between Java MouseListener and Java MotionListener?
5.3.
How many methods are present in Java MouseListener?
5.4.
What is the difference between Java MouseAdaptor and Java MouseListener?
5.5.
Which command does not generate any events?
6.
Conclusion
Last Updated: Mar 27, 2024
Medium

Java MouseListener

Author yuvatimankar
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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.

Also Read About, Multithreading in java and Hashcode Method in Java

Java MouseListener

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
Run Code

Methods of Java MouseListener

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
Run Code

 

Output:

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.

Java MouseListener Output

This is the output generated when we exited the mouse from the output frame.

Java MouseListener Output

And, This is the output generated when we have clicked the mouse on the output frame.

Try and compile it by yourself on java online compiler.

Code 2:

In this example, we will how a JavaMouseListener works when we click on the frame.

import java.awt.*;  
import java.awt.event.*;  
public class ninja extends Frame implements MouseListener{  
    MouseListenerExample2(){  
        addMouseListener(this);  
          
        setSize(300,300);  
        setLayout(null);  
        setVisible(true);  
    }  
    public void mouseClicked(MouseEvent e) {  
        Graphics g=getGraphics();  
        g.setColor(Color.RED);  
        g.fillOval(e.getX(),e.getY(),30,30);  
    }  
    public void mouseEntered(MouseEvent e) {}  
    public void mouseExited(MouseEvent e) {}  
    public void mousePressed(MouseEvent e) {}  
    public void mouseReleased(MouseEvent e) {}  
      
public static void main(String[] args) {  
    new ninja();  
}  
} 
You can also try this code with Online Java Compiler
Run Code

 

Output:

Java MouseListener Output

This output is generated when we enter the mouse in the frame.

Read More About, Basics of Java

 

Code 3:

import java.awt.*;  
import java.awt.event.*;  
public class Ninja extends Frame implements MouseListener{  
    Label l;  
   Ninja(){  
        addMouseListener(this);  
          
        l=new Label();  
        l.setBounds(20,50,100,20);  
        add(l);  
        setSize(300,300);  
        setLayout(null);  
        setVisible(true);  
    }  
    public void mouseClicked(MouseEvent e) {  
        l.setText("Mouse Clicked");  
    }  
    public void mouseEntered(MouseEvent e) {  
        l.setText("Mouse Entered");  
    }  
    public void mouseExited(MouseEvent e) {  
        l.setText("Mouse Exited");  
    }  
    public void mousePressed(MouseEvent e) {  
        l.setText("Mouse Pressed");  
    }  
    public void mouseReleased(MouseEvent e) {  
        l.setText("Mouse Released");  
    }  
public static void main(String[] args) {  
    new Ninja();  
}  
}
You can also try this code with Online Java Compiler
Run Code

 

Output:

Java MouseListener Output

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.

You can also read about the Multiple Inheritance in Java.

Frequently Asked Questions

What is the purpose of Java MouseListener?

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. 

If you want to learn more on such topics, you can see, Introduction to JavaBasics of JavaLearn getting started with Java, and Learn OOPs in Java, also you can enroll in the course Basics of Java with Data structure and algorithms. Also, you can visit our website, code studio!

You can also refer to our guided path on Coding Ninjas Studio to upskill yourself in Data structure and algorithmsCompetitive Programming, Javascriptand System Design.

Do upvote our blogs if you find them helpful and engaging!

Happy learning!

Live masterclass