Home » Desktop Java » awt » event » Handle mouse clicks example

About Byron Kiourtzoglou

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.

Handle 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.
  • Alternatively you can use MouseEvent.getPoint().x and  MouseEvent.getPoint().y to get the coordinates.
  • 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.BorderLayout;
import java.awt.Component;
import java.awt.Frame;
import java.awt.TextArea;
import java.awt.event.InputEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class MouseListener {

  public static void main(String[] args) {

// Create frame with specific title

Frame frame = new Frame("Example Frame");

// Create a component to add to the frame; in this case a text area with sample text

Component textArea = new TextArea("Click here to see mouse click info...");

// Add a mouse listener to capture click events

textArea.addMouseListener(new MouseAdapter() {

  public void mouseClicked(MouseEvent evt) {

    TextArea source = (TextArea) evt.getSource();

    if ((evt.getModifiers() & InputEvent.BUTTON1_MASK) != 0) {

  source.setText(source.getText() + "nLeft mouse button clicked on point [" + evt.getPoint().x + "," + evt.getPoint().y + "]");

    }

    if ((evt.getModifiers() & InputEvent.BUTTON2_MASK) != 0) {

source.setText(source.getText() + "nCenter mouse button clicked on point [" + evt.getPoint().x + "," + evt.getPoint().y + "]");

    }

    if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) != 0) {

source.setText(source.getText() + "nRight mouse button clicked on point [" + evt.getPoint().x + "," + evt.getPoint().y + "]");

    }

  }

});

// Add the components to the frame; by default, the frame has a border layout

frame.add(textArea, BorderLayout.NORTH);

// Show the frame

int width = 300;

int height = 300;

frame.setSize(width, height);

frame.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