Introduction
This article explains the Mouse Listener class in Java.
What the Mouse Listener is?
When a user interacts with a component using the mouse an event is generated called a mouse event since it is generated by the mouse. It is generated when we are working with the mouse, like clicking somewhere or releasing or pressing the mouse, etcetera. For tracking the mouse cursor Java provides another separate interface called MOuseMotionListener. For tracking the wheel position of the mouse Java provides MouseWheel Listener.
Mouse Event Methods
There are five public methods in this class that are:
- void mouseClicked(MouseEvent mevnt)
It is called when the user clicks somewhere in the window.
- void mouseEntered(MouseEvent mevnt)
It is called when the cursor is inside the region of the window.
- void mouseExited(MouseEvent mevnt)
It is called when the cursor leaves the region of the window.
- void mousePressed(MouseEvent mevnt)
It is called when the user clicks or presses a mouse button.
- void mouseReleased(MouseEvent mevnt)
It is called when the user releases a mouse button.
Examples
1.MouseEventEx1.java
In this example; we create a Java class that shows the MouseEvent functionality, such as it shows the location of the mouse when clicked, moved, dragged and when the mouse is moved outside it shows that the mouse has gone outside of the window. In this class we implement two listeners, MouseMotionListener and MouseListener. MouseMotionListener shows the location of a movable mouse pointer and the other interface shows the other events of the mouse.
- import javax.swing.*;
- import java.awt.event.*;
- import java.awt.*;
- public class MouseEventEx1 extends JFrame implements MouseMotionListener, MouseListener
- {
- private JLabel jlabl;
- public MouseEventEx1()
- {
- super("Mouse Tracker In Java");
- jlabl = new JLabel();
- jlabl.setBackground(Color.gray);
- jlabl.setForeground(Color.blue);
- jlabl.setFont(new Font("Segoe UI", Font.BOLD, 18));
- getContentPane().add(jlabl, BorderLayout.SOUTH);
- setSize(600, 350);
- setLocation(250, 250);
- addMouseMotionListener(this);
- addMouseListener(this);
- show();
- }
- public void mouseClicked(MouseEvent menvt)
- {
- jlabl.setText("Clicked at following location [" + menvt.getX() + ", " + menvt.getY() + "]");
- }
- public void mousePressed(MouseEvent menvt)
- {
- jlabl.setText("Pressed at following location [" + menvt.getX() + ", " + menvt.getY() + "]");
- }
- public void mouseReleased(MouseEvent menvt)
- {
- jlabl.setText("Released at following location [" + menvt.getX() + ", " + menvt.getY() + "]");
- }
- public void mouseEntered(MouseEvent menvt)
- {
- jlabl.setText("Mouse still inside window");
- }
- public void mouseExited(MouseEvent menvt)
- {
- jlabl.setText("Mouse went outside window");
- }
- public void mouseDragged(MouseEvent menvt)
- {
- jlabl.setText("Dragged at following location [" + menvt.getX() + ", " + menvt.getY() + "]");
- }
- public void mouseMoved(MouseEvent menvt)
- {
- jlabl.setText("Moved at following location [" + menvt.getX() + ", " + menvt.getY() + "]");
- }
- public static void main(String args[])
- {
- MouseEventEx1 applcn = new MouseEventEx1();
- applcn.addWindowListener(new WindowAdapter()
- {
- public void windowClosing(WindowEvent menvt)
- {
- System.exit(0);
- }
- });
- }
- }
Output
When we press Enter we get a new window and the following figure shows the location of the movable mouse's current position.
When we press the mouse somewhere in a new window it shows the location as in the following. Similarly, we can do another event also, like mouse dragging.
2. MouseEventEx2.java
In this example; we create a frame for the purpose of drawing or painting. In this example, we use a Swing component like graphics to design or to provide a way to paint something on the frame.
- import java.awt.event.*;
- import javax.swing.*;
- import java.awt.*;
- public class MouseEventEx2 extends JFrame
- {
- private int xVal = -10, yVal = -10;
- Public MouseEventEx2()
- {
- super("Painting In Java");
- getContentPane().add(new Label("Starts your painting by dragging mouse"), BorderLayout.NORTH);
- addMouseMotionListener
- (new MouseMotionAdapter()
- {
- public void mouseDragged(MouseEvent mevnt) {
- xVal = mevnt.getX();
- yVal = mevnt.getY();
- repaint();
- }
- });
- setSize(630, 350);
- setLocation(350, 350);
- show();
- }
- public void paint(Graphics grp)
- {
- grp.fillOval(xVal, yVal, 5, 5);
- }
- public static void main(String args[])
- {
- MouseEventEx2 applcn = new MouseEventEx2();
- applcn.addWindowListener(new WindowAdapter() {
- public void windowClosing(WindowEvent mevnt) {
- System.exit(0);
- }
- });
- }
- }
Output
After pressing enter we get a new frame where we can draw anything using the mouse.
The following figure shows a demonstration of painting in Java using the WindowEvent class.
3. MouseEventEx3.java
In this example; we create a frame to show the perfect location of the mouse clicked and also shows which button you used, like left, right or center button.
- import java.awt.event.*;
- import javax.swing.*;
- import java.awt.*;
- public class MouseEventEx3 extends JFrame
- {
- private int xCord, yCord;
- private String str = "";
- public MouseEventEx3()
- {
- addMouseListener(new HandlingClick());
- setSize(600, 350);
- setLocation(350, 350);
- show();
- }
- public void paint(Graphics grp)
- {
- grp.drawString("You clicled @ [" + xCord + ", " + yCord + "] ", xCord, yCord);
- }
- public static void main(String args[])
- {
- MouseEventEx3 applcn = new MouseEventEx3();
- applcn.addWindowListener(new WindowAdapter()
- {
- public void windowClosing(WindowEvent mevnt)
- {
- System.exit(0);
- }
- });
- }
- private class HandlingClick extends MouseAdapter
- {
- public void mouseClicked(MouseEvent mevnt)
- {
- xCord = mevnt.getX();
- yCord = mevnt.getY();
- String str = "You clicked " + mevnt.getClickCount();
- str += " time(str)";
- if (mevnt.isMetaDown())
- str += " using right button of mouse";
- else if (mevnt.isAltDown())
- str += " using center button of mouse";
- else
- str += " using left button of mouse";
- setTitle(str);
- repaint();
- }
- }
- }
Output
After pressing enter we get a new frame which displays the coordinates of the mouse click.
The following figure shows the clicking event as when first we click using the left button of mouse it shows coordinates (207, 140) and again we click the right button of the mouse and it shows on the title that you are clicking the right button and shows the coordinates (208,176).