CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > Swing > Mouse Event in Java Swing

Swing Tutorial

  • What Is Java Swing
  • First Program
  • What is AWT
  • Advantages of Swing Over AWT
  • JFrame
  • Swing Components
  • JLabel
  • JTextField
  • JPasswordField
  • JButton
  • Event Handling
  • FlowLayout
  • BorderLayout
  • GridLayout
  • GridBagLayout
  • BoxLayout
  • JPanel
  • JCheckBox
  • JRadioButton
  • Border Component
  • JComboBox
  • JList
  • JList with MVC
  • Mouse Event
  • Key Event
  • JMenuBar
  • JTextArea
  • Using Color
  • Use Font
  • Display Image/Icon
  • Dialog Box
  • JTable
  • JDesktopPane and JInternalFrame
  • JDesktopPane
  • JInternalFrame
  • Adapter Classes
  • Swing Timer
  • JScrollPane
  • JSlider
  • JProgressBar
  • JSeparator
  • JToolBar
  • ActionListener
  • ItemListener
  • Worker Thread
  • Nimbus Look and Feel

Mouse Event in Java Swing

Learn the concepts, implementation details, and practical steps with a clean developer-focused walkthrough.

Yuba Raj Kalathoki
By Yuba Raj Kalathoki
Last updated: July 1, 2026 · 2 min read · 0 Comments
Share: in X

In Java Swing, we can capture mouse events such as clicking, dragging, and releasing the mouse using the MouseListener and MouseMotionListener interfaces.

Following is an example code that demonstrates how to capture mouse events in Java Swing:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;

public class MouseEventDemo extends JFrame implements MouseListener, MouseMotionListener {
	private JLabel label;

	public MouseEventDemo() {
		super("MouseEvent Demo");
		label = new JLabel("No mouse event captured yet.", SwingConstants.CENTER);
		label.setPreferredSize(new Dimension(300, 100));
		add(label, BorderLayout.CENTER);
		addMouseListener(this);
		addMouseMotionListener(this);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		pack();
		setLocationRelativeTo(null);
		setVisible(true);
	}

	@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() + ")");
	}

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

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

	public static void main(String[] args) {
		SwingUtilities.invokeLater(() -> new MouseEventDemo());
	}

}

In the example above, we create a JFrame and add a JLabel to display the mouse events captured. We implement the MouseListener and MouseMotionListener interfaces and override their methods to capture the different mouse events. We then register the frame as a listener for mouse events using the addMouseListener and addMouseMotionListener methods. Finally, we set the frame visible and pack it to the preferred size.

When the user clicks, presses, releases, enters, or exits the mouse, or drags or moves the mouse, the corresponding method will be called and the label’s text will be updated with the coordinates of the mouse event.

Sample output:

mouseevent demo

Related Posts:

  • What Is Java Swing? A Complete Guide to Java’s GUI Toolkit
  • Control Statements in Java
  • Interface in Java: Mastering Abstraction and…
  • MySQL Commands for Developers
  • Packages in Java: A Guide to Modular, Maintainable Code
  • Identifiers in Java
Tags:guijavaswing
Was this article helpful?
← Previous ArticleStack Class in Java
Next Article →Dictionary Class in Java

Recent Posts

  • How to implement Passwordless Authentication in Spring Boot: A Step-by-Step Guide
  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • How to Fix SSH Agent Forwarding on macOS: The Ultimate Guide for Developers
  • How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)
  • How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error
CoderSathi

Your go-to resource for Java, Spring Boot, Microservices, AWS, and modern development tutorials.

Linkedin

Quick Links

  • About
  • Contact

Popular Topics

  • Java
  • Spring Boot
  • AWS
  • DevOps
  • MongoDB
  • Linux
  • Git
  • How to
© 2026 CoderSathi. All rights reserved. Privacy Policy · Sitemap