Create JFrame window with mouse event handling

This is an example that demonstrates how to create a JFrame windows that supports mouse event handling. This is very useful in most GUI applications because most of the time it is very efficient for the user to provide input using his mouse. Additionally, you can make your application behave accordingly to mouse movement and generally to mouse events.

Basically all you have to do in order to create a JFrame that handles mouse events is:

  • Create a class that extends JFrame and implements MouseListener.
  • Overrde mouseClicked, mouseEntered, mouseExited, mousePressed, mouseReleased to monitor the coresponding events. Now every time one of these events occures, the respective function will fire up.
  • Use MouseEvent.getX(), MouseEvent.getY() to get the coordinates of the window that the mouse event occurs.

Let’s see the code:

package com.javacodegeeks.snippets.desktop;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JFrame;

public class CreateJFrameWindowWithMouseEventHandling extends JFrame implements MouseListener {

	private static final long serialVersionUID = 1L;

	public CreateJFrameWindowWithMouseEventHandling() {
		setTitle("Simple Frame");

	public void mouseClicked(MouseEvent e) {
		int x = e.getX();
		int y = e.getY();
		System.out.println("Mouse Clicked at X: " + x + " - Y: " + y);

	public void mouseEntered(MouseEvent e) {
		int x = e.getX();
		int y = e.getY();
		System.out.println("Mouse Entered frame at X: " + x + " - Y: " + y);

	public void mouseExited(MouseEvent e) {
		int x = e.getX();
		int y = e.getY();
		System.out.println("Mouse Exited frame at X: " + x + " - Y: " + y);

	public void mousePressed(MouseEvent e) {
		int x = e.getX();
		int y = e.getY();
		System.out.println("Mouse Pressed at X: " + x + " - Y: " + y);

	public void mouseReleased(MouseEvent e) {
		int x = e.getX();
		int y = e.getY();
		System.out.println("Mouse Released at X: " + x + " - Y: " + y);

	private static void createAndShowGUI() {

  //Create and set up the window.

  JFrame frame = new CreateJFrameWindowWithMouseEventHandling();

  //Display the window.




	public static void main(String[] args) {

  //Schedule a job for the event-dispatching thread:

  //creating and showing this application's GUI.

  javax.swing.SwingUtilities.invokeLater(new Runnable() {

public void run() {





This was an example on how to create JFrame window with mouse event handling.

Want to know how to develop your skillset to become a Java Rockstar?

Join our newsletter to start rocking!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions


Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Newest Most Voted
Inline Feedbacks
View all comments
4 years ago

Tested, it works fine. Thank you!

Back to top button