File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Current mouse location... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Current mouse location..." Watch "Current mouse location..." New topic

Current mouse location...

G Estes
Ranch Hand

Joined: Oct 22, 2002
Posts: 62
How would you capture the current mouse location at a given time? Specifically, the Point on the screen where the mouse pointer is located.
Rene Liebmann
Ranch Hand

Joined: May 02, 2002
Posts: 196
If you have a component, then this component can catch every mouse event, for example mouse moved event. This event keeps the information of the current position. You can hold this information in a member (Point) and if the time comes, where you need the information, you will have it. This is the way I would go.
Jawad Kakar
Ranch Hand

Joined: Oct 06, 2002
Posts: 82
if e is MouseEvent then
int x = e.getX();
int y = e.getY();
would be the point where the mouse was clicked.
G Estes
Ranch Hand

Joined: Oct 22, 2002
Posts: 62
I guess I wasn't clear. If the mouse is not clicked, and not over my component and the user presses some key (F6 for instance), at that point I want to use the Robot class to do some things with the mouse, move it, click some things, etc...and then return it to it's original location. So what I need is a way to get the original (current) location of the mouse without having to depend on the MouseEvent.
Nathan Pruett

Joined: Oct 18, 2000
Posts: 4121

If the mouse is not in a component listening for mouse events (i.e. if it's hanging out on the windows desktop outside of your application) then there's nothing you can do to get the mouse location. Searching for resources on this led me to find there used to be a class called EventQueueMonitor in some really early com.sun package for Java Accessability that did this, but there is no way currently provided to do this...

Write once, run anywhere, because there's nowhere to hide! - /. A.C.
I agree. Here's the link:
subject: Current mouse location...
It's not a secret anymore!