my dog learned polymorphism*
The moose likes Swing / AWT / SWT and the fly likes Obtaining position of cursor on JPanel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Obtaining position of cursor on JPanel" Watch "Obtaining position of cursor on JPanel" New topic
Author

Obtaining position of cursor on JPanel

colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
Hi, I know how to use MouseListener and MouseMotionListener,
to react to MouseEvents such as pressing the mouse etc.

Then from within mousePressed(MouseEvent e), I can easily grab the
coordinates of the mouse using e.getX(), and e.getY(),

But can I just grab the coordinates of the mouse from where it is?
Without having to press/click/drag/release etc.

Thanks
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
Ok, I found this..



Not sure exactly how it works, does it give the location of the
mouse in whichever JPanel or Component its in?
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
Hi, is there a better way to get the cursor position of mouse pointer inside a JPanel.

I'm thinking of using...


Then the difference in X and Y values should be the cursor position inside of the Component (assuming mouse is actually inside the component)

But will this still work for an applet which lies inside a browser?
It would take me quite a while to set the applet up, so I thought I would just ask first before I try experimenting.

Thanks
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Try overriding mouseMoved in your MouseMotionListener.
e.getPoint gives the mouse location in the source component coordinate system.
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
Thats what I normally do, but that method only gets triggered when the mouse is moved, I want to get the coordinate after the completion of a certain process.

I seem to have it working fine, but I'm not sure if it will work in an applet or not, I guess it will
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Obtaining position of cursor on JPanel
 
Similar Threads
How to reset jpanel...
How to add mouse listener?
Wanted: example mouse coordinates->world coordinates
Get the mouse co ordiantes for the drop down list value
CSPRNG