Curriculum
Course: Learn Java Programming
Login

Curriculum

Learn Java Programming

Text lesson

Implementing the Mouse Event using MouseListener and MouseMotionListener in Java

[post-views]

 

 

In this lesson, you will learn.

  • MouseEvent Class
  • MouseListener Interface
  • Examples

 

To handle mouse events, you must implement the MouseListener and the MouseMotionListener interfaces.

 

MouseEvent Class

  1. MouseEvent is a class that encapsulates information about mouse events, such as mouse clicks, movements, and button actions.
  2. It provides methods to get details about the event, such as the cursor’s location, where the mouse button was pressed, and the number of clicks.

 

MouseEvent Methods

Method Description
int getX() Returns the x-coordinate of the mouse event relative to the source component.
int getY() Returns the y-coordinate of the mouse event relative to the source component.
Point getPoint() Returns the Point object representing the x and y coordinates of the mouse event.
int getButton() Returns which, if any, of the mouse buttons has changed state.
int getClickCount() Returns the number of mouse clicks associated with the event.

 

MouseListener Interface

MouseListener is an interface that listens for mouse events. It has five methods that must be implemented.

 

MouseListener Methods

Method Description
void mouseClicked(MouseEvent e) Invoked when the mouse button has been clicked (pressed and released) on a component.
void mousePressed(MouseEvent e) Invoked when a mouse button has been pressed on a component.
void mouseReleased(MouseEvent e) Invoked when a mouse button has been released on a component.
void mouseEntered(MouseEvent e) Invoked when the mouse enters a component.
void mouseExited(MouseEvent e) Invoked when the mouse exits a component.

 

Example: Implementing MouseListener Methods

package mouseeventhandling;

import javax.swing.*;
import java.awt.event.*;

public class MouseListenerExample extends JFrame implements MouseListener {

	private JLabel label;

	public MouseListenerExample() {
		// Set up the frame
		setTitle("MouseListener Example");
		setSize(400, 300);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLayout(null);

		// Set up the label
		label = new JLabel("Mouse events will be displayed here");
		label.setBounds(50, 100, 300, 50);
		add(label);

		// Add MouseListener to the frame
		addMouseListener(this);
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		label.setText("Mouse Clicked at (" + e.getX() + ", " + e.getY() + ")");
	}

	@Override
	public void mousePressed(MouseEvent e) {
		label.setText("Mouse Pressed at (" + e.getX() + ", " + e.getY() + ")");
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		label.setText("Mouse Released at (" + e.getX() + ", " + e.getY() + ")");
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		label.setText("Mouse Entered at (" + e.getX() + ", " + e.getY() + ")");
	}

	@Override
	public void mouseExited(MouseEvent e) {
		label.setText("Mouse Exited at (" + e.getX() + ", " + e.getY() + ")");
	}

	public static void main(String[] args) {
		MouseListenerExample example = new MouseListenerExample();
		example.setVisible(true);
	}
}

 

Output:

 

 

Explanation

  1. Imports:
    • javax.swing.*: For Swing components like JFrame and JLabel.
    • java.awt.event.*: For event handling, including MouseListener and MouseEvent.
  2. Class Declaration:
    • MouseListenerExample class extends JFrame and implements MouseListener.
  3. Constructor:
    • Sets up the JFrame with a title, size, close operation, and layout.
    • Adds a JLabel to display mouse events.
    • Registers the class as a mouse listener to the JFrame using addMouseListener(this).
  4. Override MouseListener Methods:
    • Each method updates the JLabel with the event type and the coordinates of the mouse pointer when the event occurs.
  5. Main Method:
    • Call the class.

 

Example-2: Implement MouseMotionListener

 

Methods of MouseMotionListener

Method Description
void mouseDragged(MouseEvent e) Called when a mouse button is pressed and the mouse is dragged.
void mouseMoved(MouseEvent e) Called when the mouse is moved with no buttons pressed.

 

package mouseeventhandling;

import javax.swing.*;
import java.awt.event.*;

public class MouseMotionListenerExample extends JFrame 
implements MouseMotionListener {

	private JLabel label;

	public MouseMotionListenerExample() {
		// Set up the frame
		setTitle("MouseMotionListener Example");
		setSize(300, 300);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLayout(null);

		// Set up the label
		label = new JLabel("Move the mouse within the frame");
		label.setBounds(50, 100, 300, 50);
		add(label);

		// Add MouseMotionListener to the frame
		addMouseMotionListener(this);
	}

	@Override
	public void mouseDragged(MouseEvent e) {
		label.setText("Mouse Dragged to (" + e.getX() + ", " + e.getY() + ")");
	}

	@Override
	public void mouseMoved(MouseEvent e) {
		label.setText("Mouse Moved to (" + e.getX() + ", " + e.getY() + ")");
	}

	public static void main(String[] args) {

		MouseMotionListenerExample example = new MouseMotionListenerExample();
		example.setVisible(true);
	}
}

 

Output

 

 

 


 

End of the lesson….enjoy learning

 

 

Student Ratings and Reviews

 

 

 

There are no reviews yet. Be the first one to write one.

 

 

Submit a Review

 

 

 

Layer 1
Login Categories