Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

What is a Mouse Event, Mouse Motion Event, Mouse Wheel event and how to handle them in Java?

In this article, first, we will learn about Mouse Event, Mouse Motion Event and Mouse Wheel Event. We will also learn, how to receive them from the screen and invoke some actions based on the Events with some sample codes.

In order to achieve this, we need to implement some interfaces. They are MouseListener Interface, MouseMotionListener Interface and MouseWheel Listener Interface.

MouseListener Interface is used for receiving mouse events like mouse key press, mouse key release, mouse click, mouse enter and mouse exit on a component like Frame etc.

MouseMotionListener Interfaces is used for receiving mouse motion events like mouse drag, mouse move etc.

MouseWheel Listener Interface is used for receiving Mouse Wheel event, that is mouse wheel moved.

We will look at that one by one in more detail by means of some sample code.

Step 1:

In this step we will write a code to receive mouse events and get some interesting outputs.

Listing 1: Code to execute Step 1

import java.awt.Color;
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class MouseMovements implements  MouseListener{

    JFrame frame;
    JLabel jLabel;
    public void tellmouseMovement() {
        frame = new JFrame();
        frame.setSize(500, 120);
        frame.setLocation(100, 200);
        frame.setTitle("Example Frame");
        frame.addMouseListener(this);
      
        jLabel = new JLabel();
        jLabel.setText("No Action ---" + "The size of the frame is ---" + frame.getWidth() + "," + frame.getHeight());
        frame.add(jLabel);
        frame.setVisible(true);
    }
    @Override
    public void mouseClicked(MouseEvent e) {
        frame.setSize(700, 500);
        e.getX();
        jLabel.setText("Mouse Clicked ---" + "The size of the frame is ---" + frame.getWidth() + "," + frame.getHeight());
        System.out.println("Mouse Clicked ---" + "The size of the frame is ---" + frame.getWidth() + "," + frame.getHeight());
        System.out.println("Mouse x Postion:" + e.getX());
        System.out.println("Mouse y Postion:" + e.getY());
    }
    @Override
    public void mousePressed(MouseEvent e) {
        System.out.println("Mouse pressed inside the frame");
        System.out.println("Mouse x Postion:" + e.getX());
        System.out.println("Mouse y Postion:" + e.getY());
    }
    @Override
    public void mouseReleased(MouseEvent e) {
        System.out.println("Mouse released inside the frame");
        System.out.println("Mouse x Postion:" + e.getX());
        System.out.println("Mouse y Postion:" + e.getY());
    }
    @Override
    public void mouseEntered(MouseEvent e) {
        frame.setSize(500, 400);
        jLabel.setText("Mouse Entered ---" + "The size of the frame is ---" + frame.getWidth() + "," + frame.getHeight());
        System.out.println("Mouse Entered ---" + "The size of the frame is ---" + frame.getWidth() + "," + frame.getHeight());
        System.out.println("Mouse x Postion:" + e.getX());
        System.out.println("Mouse y Postion:" + e.getY());
    }
    @Override
    public void mouseExited(MouseEvent e) {
        frame.setSize(500, 500);
        jLabel.setText("Mouse Exited --- " + "The size of the frame is ---" + frame.getWidth() + "," + frame.getHeight());
        System.out.println("Mouse Exited --- " + "The size of the frame is ---" + frame.getWidth() + "," + frame.getHeight());
        System.out.println("Mouse x Postion:" + e.getX());
        System.out.println("Mouse y Postion:" + e.getY());
    }

    public static void main(String args[]) {
        MouseMovements eventobject = new MouseMovements();
        eventobject.tellmouseMovement();
    }

 
}

When the program is run we will get a frame. When actions are performed inside the frame we will get different outputs.

The frame will change its size, as and when an action is perfomed.

Shows the screenshot of the example frame created using listing 1 (Different frame sizes1
Shows the screenshot of the example frame created using listing 1 (Different frame sizes2
Shows the screenshot of the example frame created using listing 1 (Different frame sizes3

Figure 1: Shows the screenshot of the example frame created using listing 1 (Different frame sizes)

Running this program will give us the following output in the console:

Mouse Entered ---The size of the frame is ---500,400
Mouse x Postion:427
Mouse y Postion:115
Mouse pressed inside the frame
Mouse x Postion:427
Mouse y Postion:104
Mouse released inside the frame
Mouse x Postion:427
Mouse y Postion:104
Mouse Clicked ---The size of the frame is ---700,500
Mouse x Postion:427
Mouse y Postion:104
Mouse Exited --- The size of the frame is ---500,500
Mouse x Postion:748
Mouse y Postion:55

In the above output, the program is capturing the mouse events along with mouse x and y positions. The output may vary according to the actions that is performed and the postion of mouse .

Step 2:

In this step we will write a code to receive mouse motion events and get some interesting outputs.

Listing 2: Code to execute Step 2

import java.awt.Color;
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class MoreMouseMovements implements MouseMotionListener{
    JFrame frame;
    JLabel jLabel;

    public void tellmouseMovement() {
        frame = new JFrame();
        frame.setSize(500, 120);
        frame.setLocation(100, 200);
        frame.setTitle("Example Frame");
        frame.addMouseMotionListener(this);
        jLabel = new JLabel();
        jLabel.setText("No Action ---" + "The size of the frame is ---" + frame.getWidth() + "," + frame.getHeight());
        frame.add(jLabel);
        frame.setVisible(true);
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        jLabel.setText("Mouse Dragged ---" + "The size of the frame is ---" + frame.getWidth() + "," + frame.getHeight());
        System.out.println("Mouse Dragged ---" + "The size of the frame is ---" + frame.getWidth() + "," + frame.getHeight());
        System.out.println("Mouse x Postion:" + e.getX());
        System.out.println("Mouse y Postion:" + e.getY());
    }

    @Override
    public void mouseMoved(MouseEvent e) {
        jLabel.setText("Mouse Moved ---" + "The size of the frame is ---" + frame.getWidth() + "," + frame.getHeight());
        System.out.println("Mouse Moved ---" + "The size of the frame is ---" + frame.getWidth() + "," + frame.getHeight());
        System.out.println("Mouse x Postion:" + e.getX());
        System.out.println("Mouse y Postion:" + e.getY());
    }

    public static void main(String args[]) {
        MoreMouseMovements eventobj = new MoreMouseMovements();
        eventobj.tellmouseMovement();
    }


}

When the program is run we will get a frame similar to the above screenshot and works exactly as the above frame, but for different mouse events like mouse move and mouse drag.

Running this program will give us the following output in the console:

Mouse Moved ---The size of the frame is ---500,120
Mouse x Postion:453
Mouse y Postion:83
Mouse Moved ---The size of the frame is ---500,120
Mouse x Postion:454
Mouse y Postion:79
Mouse Dragged ---The size of the frame is ---500,120
Mouse x Postion:447
Mouse y Postion:105
Mouse Dragged ---The size of the frame is ---500,120
Mouse x Postion:447
Mouse y Postion:104

Step 3:

In this step we will write a code to receive mouse wheel movements and get some interesting outputs.

Listing 3: Code to execute Step 3:

import java.awt.Color;
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class MouseWheelMovements implements MouseWheelListener{

    JFrame frame;
    JLabel jLabel;

    public void tellmouseMovement() {
        frame = new JFrame();
        frame.setSize(500, 120);
        frame.setLocation(100, 200);
        frame.setTitle("Example Frame");

        frame.addMouseWheelListener(this);
        jLabel = new JLabel();
        jLabel.setText("No Action ---" + "The size of the frame is ---" + frame.getWidth() + "," + frame.getHeight());
        frame.add(jLabel);
        frame.setVisible(true);
    }
    
   @Override
    public void mouseWheelMoved(MouseWheelEvent e) {
         frame.setSize(500, 100);
        jLabel.setText("Mouse Wheel Moved --- " + "The size of the frame is ---" + frame.getWidth() + "," + frame.getHeight());
        System.out.println("Mouse Wheel Moved --- " + "The size of the frame is ---" + frame.getWidth() + "," + frame.getHeight());
         System.out.println("Mouse x Postion:" + e.getX());
            System.out.println("Mouse y Postion:" + e.getY());
    }
    public static void main(String args[]) {
        MouseWheelMovements eventobj = new MouseWheelMovements();
        eventobj.tellmouseMovement();
    }

 
}

When the program is run we will get a frame similar to the above screenshot and works exactly as the above frame, but for mouse wheel movement.

Running this program will give us the following output:

Mouse Wheel Moved --- The size of the frame is ---500,100
Mouse x Postion:395
Mouse y Postion:86

Conclusion:

In this article, we have learnt about about Mouse Event , Mouse Motion Event, Mouse Wheel Event and also learnt, how to handle them.



I am well versed with Computer Programming languages and possess good working knowledge on software languages such as C, Java, PHP, HTML and CSS

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login