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 mouse
Take a look at Component.getMousePosition.
Returns the position of the mouse pointer in this
Component's coordinate space if theComponentis directly under the mouse pointer, otherwise returnsnull. If theComponentis not showing on the screen, this method returns null even if the mouse pointer is above the area where theComponentwould be displayed. If theComponentis partially or fully obscured by otherComponents or native windows, this method returns a non-nullvalue only if the mouse pointer is located above the unobscured part of theComponent.
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;
}