21

Say I'm in a Java Swing JFrame. I click my mouse. I want to get the location of the mouse click within the GUI. In java, the line

int mouseX = MouseInfo.getPointerInfo().getLocation.x;

Seems to give the location of the mouse on the entire screen. How would I get it's location relative to the GUI?

pipsqueaker117
  • 2,280
  • 9
  • 35
  • 47
  • Try using [`MouseEvent.getPoint`](http://docs.oracle.com/javase/7/docs/api/java/awt/event/MouseEvent.html#getPoint()) – obataku Sep 12 '12 at 20:55
  • have to look at SwingUtilities, for determine the real mouse locations and relative to JFrame – mKorbel Sep 13 '12 at 05:37

4 Answers4

25

From MouseListener methods you can do:

@Override
public void mouseClicked(MouseEvent e) {
    int x=e.getX();
    int y=e.getY();
    System.out.println(x+","+y);//these co-ords are relative to the component
}

Simply add this to your Component by:

component.addMouseListener(new MouseListener() {
    @Override
    public void mouseClicked(MouseEvent e) {
    }
});

Reference:

David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
  • 2
    AFAIK, **getX()/getY()** they both return an `int value`, so there never appears a reason to cast the value explicitly as you doing in your case :-), though +1 for the rest. – nIcE cOw Sep 13 '12 at 01:38
  • Yes, but what if you wanted to get the location of the mouse relative to the JFrame, without having to first click? – Cody Richardson Jun 28 '16 at 02:53
  • @CodyRichardson with the MouseMotionListener, same concept as in the example but with methods mouseMoved and mouseDragged – matt Dec 01 '16 at 13:41
10

Take a look at Component.getMousePosition.

Returns the position of the mouse pointer in this Component's coordinate space if the Component is directly under the mouse pointer, otherwise returns null. If the Component is not showing on the screen, this method returns null even if the mouse pointer is above the area where the Component would be displayed. If the Component is partially or fully obscured by other Components or native windows, this method returns a non-null value only if the mouse pointer is located above the unobscured part of the Component.

final Point mousePos = component.getMousePosition();
if (mousePos != null) {
  final int mouseX = mousePos.x;
  final int mouseY = mousePos.y;
  ...
}

... or, if you use a MouseListener, you may see my original comment...

Try using MouseEvent.getPoint.

The above will return the mouse point relative to the component to which the listener was bound.

public void mouseClicked(final MouseEvent evt) {
  final Point pos = evt.getPoint();
  final int x = pos.x;
  final int y = pos.y;
}
Community
  • 1
  • 1
obataku
  • 29,212
  • 3
  • 44
  • 57
8

You can add MouseListener to GUI component whose top left pixel should be threated as [0,0] point, and get x and y from MouseEvent

JFrame frame = new JFrame();
JPanel panel = new JPanel();
frame.add(panel);
panel.addMouseListener(new MouseAdapter() {// provides empty implementation of all
                                           // MouseListener`s methods, allowing us to
                                           // override only those which interests us
    @Override //I override only one method for presentation
    public void mousePressed(MouseEvent e) {
        System.out.println(e.getX() + "," + e.getY());
    }
});
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.setVisible(true);
Pshemo
  • 122,468
  • 25
  • 185
  • 269
3

MouseEvent has methods getX() and getY() that return the position relative to the source component.

Mark
  • 28,783
  • 8
  • 63
  • 92
  • See also [*How to Write a Mouse Listener*](http://docs.oracle.com/javase/tutorial/uiswing/events/mouselistener.html). – trashgod Sep 13 '12 at 00:41