• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Obtaining position of cursor on JPanel

 
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 750
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 750
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try overriding mouseMoved in your MouseMotionListener.
e.getPoint gives the mouse location in the source component coordinate system.
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic