• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us

How to Use Mouse Listeners in Java

September 26, 2023 //  by Talha Malik

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.

Category: Java

Previous Post: « How to Use for Loop in Java
Next Post: How to Use the getCompletedTaskCount() Method of ThreadPoolExeceutor in Java »

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

How to Use Math.min() Method in Java?

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact