Home » Desktop Java » awt » event » Handling Mouse Clicks example

About Ilias Tsagklis

Ilias Tsagklis
Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.

Handling Mouse Clicks example

In this short tutorial we are going to see how to monitor a very basic GUI event in Java Desktop Applications, which is mouse clicks. What we want to see is what button mouse the user pressed (left, middle or right click) and then we want to know the exact location, in coordinates, in our Frame that this event occurred.

In short, to handle mouse clicks in a Java AWT application :

  • Create a JFrame
  • Create a MouseAdapter and add it to the JFrame using addMouseListener method.
  • Use MouseEvent.getModifiers() and InputEvent or MouseEvent masks to see what button of the mouse the user clicked. Alternatively you can use MouseEvent.getButton() method
  • Use MouseEvent.getX() and MouseEvent.getY() to get the coordinates of the point that the user clicked into.
  • Use MouseEvent.getClickCount to get the amount of clicks that the user performed.
So, let’s see the code snippet bellow:
package com.javacodegeeks.snippets.desktop;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;
import javax.swing.JTextArea;

public class Main extends JFrame {

    public Main() {

  setSize(600, 600);

  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  final JTextArea text = new JTextArea();

  text.setText("Click inside the white area. Use all the buttons in your mouse!");

  text.addMouseListener(new MouseAdapter() {

@Override

public void mouseClicked(MouseEvent event) {

    if (event.getButton() == MouseEvent.NOBUTTON) {

  text.setText("No button clicked");

    } else if (event.getButton() == MouseEvent.BUTTON1) {

  text.setText("Button 1 clicked");

    } else if (event.getButton() == MouseEvent.BUTTON2) {

  text.setText("Button 2 clicked");

    } else if (event.getButton() == MouseEvent.BUTTON3) {

  text.setText("Button 3 clicked");

    }

    System.out.println("Number of clicks: " + event.getClickCount());

    System.out.println("Pointing at (X, Y):  " + event.getX() + ", " + event.getY());

}

  });

  getContentPane().add(text);
    }

    public static void main(String[] args) {

  new Main().setVisible(true);
    }
}

 
This was an example on how to handle mouse clicks in Java.

(No Ratings Yet)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

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

 

Leave a Reply

avatar
  Subscribe  
Notify of