Open In App

Java MouseMotionListener in AWT

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

Java AWT (Abstract Window Toolkit) provides varied event listeners to wield exploiter interactions. One of these is the Java MouseMotionListener, which is secondhand to track sneak out motion events. This hearer allows you to respond to the mouse’s front, such as sleuthing when the mouse is dragged or moved within a component. In this clause, we wish research the MouseMotionListener in Java AWT, including its syntax, methods, constructors, and a sample code with comments.

Class Declaration

The MouseMotionListener user interface is disunited of the java.awt.event box. To use it, you must follow through this user interface in your classify. Here’s the syntax of class declaration for a assort that uses the MouseMotionListener:

Java




import java.awt.event.MouseMotionListener;
import java.awt.event.MouseEvent;
  
class YourClass implements MouseMotionListener {
    // Implement the required methods
    public void mouseDragged(MouseEvent e) {
        // Your code for mouse drag event
    }
  
    public void mouseMoved(MouseEvent e) {
        // Your code for mouse move event
    }
}


MouseMotionListener Class Methods

Method

Description

public void mouseDragged(MouseEvent e)

This method is called when the mouse is dragged with a button down. It provides information about the mouse event, such as the mouse’s location and the component it occurred on.

public void mouseMoved(MouseEvent e)

This method is called when the mouse is moved without any buttons being pressed. It also provides information about the mouse event.

Example of MouseMotionListener

Below is the implementation of MouseMotionListener:

Java




import java.awt.*;
import java.awt.event.*;
  
class MouseMotionExample extends Frame implements MouseMotionListener {
    Label label;
  
    MouseMotionExample() {
        // Create a label to display mouse coordinates
        label = new Label("Mouse Coordinates: ");
  
        // Add the label to the frame
        add(label);
  
        // Register this frame as a listener for mouse motion events
        addMouseMotionListener(this);
  
        // Set the frame size and layout
        setSize(400, 400);
        setLayout(null);
  
        // Set the label's position in the middle of the window
        label.setBounds(100, 180, 200, 20);
  
        // Center-align the text in the label
        label.setAlignment(Label.CENTER);
  
        // Make the frame visible
        setVisible(true);
    }
  
    public void mouseDragged(MouseEvent e) {
        // Empty implementation for mouse drag event
    }
  
    public void mouseMoved(MouseEvent e) {
        // Get the mouse coordinates
        int x = e.getX();
        int y = e.getY();
  
        // Update the label with the current mouse coordinates
        label.setText("Mouse Coordinates: (" + x + ", " + y + ")");
    }
  
    public static void main(String[] args) {
        new MouseMotionExample();
    }
}


Output:

MouseMotionExampleOutput

Final Output of the Program:

Output of MouseMotionListener Program

Conclusion

In this article explored the usage of the MouseMotionListener in Java AWT to track mouse motion events within a graphical user interface. We covered the syntax of class declaration, the two essential methods for handling mouse motion events, and how to position a label to display the mouse coordinates in the center of the window.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads