Open In App

Java MouseListener in AWT

Last Updated : 13 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The Abstract Window Toolkit (AWT) in Java provides a collection of user interface components and event-handling features for creating graphical user interfaces (GUIs). User interaction is an important component of GUI programming, and the MouseListener interface in AWT is an important tool for managing mouse events in Java applications. In this article, we will understand about the MouseListener interface in detail.

Java AWT MouseListener

The MouseListener interface is part of the ‘java.awt.event’ package. It is used to retrieve and respond to mouse-related events in Java applications. Mouse clicks, mouse button presses and releases, mouse enter and exit events are examples of these events. By implementing the MouseListener interface, you can define specific actions that your application has to perform in response to the interactions with the mouse.

Syntax of MouseListener

The following syntax is used to declare a Java MouseListener interface:

public interface MouseListener extends EventListener

Methods of MouseListener Interface

Below is the list of all the methods associated with AWT MouseListener with its description

Method

Description

void mouseClicked(MouseEvent e)

Triggered when the mouse button is clicked (pressed and released) on a component.

void mousePressed(MouseEvent e)

Triggered when a mouse button is pressed on a component.

void mouseReleased(MouseEvent e)

This method is called when a mouse button is released on a component.

void mouseEntered(MouseEvent e)

Invoked after mouse entry into a component.

void mouseExited(MouseEvent e)

Invoked when the mouse exits a component.

Examples of Java AWT MouseListener

Here are some examples that will help you to understand the demo of the MouseListener interface easily.

Example 1:

This is a simple example to implement a MouseListener with overriding all the methods. The label is updated at every stage showing the appropriate status of the Mouse event.

Java




// Java AWT Program to demonstrate
// MouseListener
import java.awt.*;
import java.awt.event.*;
  
public class Main {
    public static void main(String[] args){
        
        // Create an instance of frame
        Frame f = new Frame("MouseListener Demo");
        
          // Create a label
        Label l = new Label("Welcome to GeeksforGeeks!");
        
          // Set the properties of label
        l.setBounds(80, 100, 220, 30);
        l.setFont(new Font("Serif", Font.BOLD, 18));
        l.setForeground(Color.GREEN);
  
        // Add MouseListener to the label with different methods
        l.addMouseListener(new MouseListener() {
            
            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");
            }
            
        });
  
        // Add label to the frame
        f.add(l);
  
        // Set the properties of frame
        f.setLayout(null);
        f.setSize(400, 300);
        f.setVisible(true);
    }
}


Commands to run MouseListener code:

javac Main.java
java Main

AWT MouseListener Output:

Output of MouseListener Example 1

Example 2:

This example is of a simple drawing application using Java AWT MouseListener. Users can draw green squares by clicking the mouse in the given Frame.

Java




// Java AWT Program to demonstrate
// MouseListener
import java.awt.*;
import java.awt.event.*;
  
public class Main implements MouseListener {
    Frame frame;
    Label l;
  
    Main(){
        
        // Create a frame
        frame = new Frame("MouseListener Example");
        
        // Create a label
        l = new Label("Welcome to GeeksforGeeks!");
        
        // Set the properties of label
        l.setBounds(70, 50, 250, 20);
        l.setAlignment(Label.CENTER);
        l.setFont(new Font("Serif", Font.BOLD, 18));
        l.setForeground(Color.GREEN);
        
        // Add label to the frame
        frame.add(l);
        
        // Add MouseListener to the frame
        frame.addMouseListener(this);
        
        // Set the properties of frame
        frame.setSize(400, 400);
        frame.setLayout(null);
        frame.setVisible(true);
    }
  
    // Override the abstract methods of MouseListener
    public void mouseClicked(MouseEvent e) {}
    public void mouseEntered(MouseEvent e) {}
    public void mouseExited(MouseEvent e) {}
  
    // When mouse is pressed, a green rectangle is drawn
    public void mousePressed(MouseEvent e){
        Graphics g = frame.getGraphics();
        g.setColor(Color.GREEN);
        g.fillRect(e.getX(), e.getY(), 30, 30);
    }
  
    public void mouseReleased(MouseEvent e) {}
  
    // Main Method
    public static void main(String[] args) { new Main(); }
}


AWT MouseListener Output:

Output of MouseListener Example 2



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads