PointerInfo a = MouseInfo.getPointerInfo();
Point b = a.getLocation();
int x = (int) b.getX();
int y = (int) b.getY();
System.out.print("Y: "+y);
System.out.print("X: "+x+"\n");

Right now I'm using this, but it gives me the coordinates relative to the entire monitor. Since you can move the program frame anywhere on the monitor, this doesn't help me. How can I get the mouse's coordinates relative to the program window? For instance, the top left corner would be (0, 0) and the bottom right would be (400, 420)

Recommended Answers

Point#getLocationOnScreen(); returns correct Container Point

Jump to Post

All 4 Replies

Point#getLocationOnScreen(); returns correct Container Point

Point b = a.getLocationOnScreen();

I changed it to this and I get an error to change to getLocation()
Does something need to be changed on PointerInfo a?

Huzzah, darwin fish for the win again, thanks Ezz
This works:

public void mouseClicked(MouseEvent e) {
	PointerInfo a = MouseInfo.getPointerInfo();
	Point point = new Point(a.getLocation());
	SwingUtilities.convertPointFromScreen(point, e.getComponent());
	x=(int) point.getX();
	y=(int) point.getY();
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.