Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to get the mouse's position

 
mira yang
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!
how i can get the mouse's position without listening a mouse event?

Thanks!
 
Fuqiang Zhao
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you use jdk1.5, you can use the MouseInfo object, for details, please visit:
http://java.sun.com/j2se/1.5.0/docs/api/java/awt/MouseInfo.html
Hope that helps!
 
mira yang
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but how to do in jdk1.4?
 
Fuqiang Zhao
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use native methods.
 
Eddie Vanda
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why would you not want to implement MouseMotionListener?
 
mira yang
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because i'm listening to a TreeSelectionEvent,and when a event occurs ,the
system want to popup a menu at the mouse's position, so i want to know the
position without listening to a mouse event.
is there any other solution?
Thanks!
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
TreeSelectionEvent provides a TreePath of the selected nodes of the tree. You can then call the following methods of JTree to get information about the coordinates of the TreePath that was selected.

- getPathBounds( TreePath ) returns a Rectangle that gives you the bounds of the path.

- getRowForPath( TreePath ) returns the index of the selected row, and then you can use getRowBounds( int ) to get the bounds of that row.

You can use either of these methods to calculate a good point to launch a popup menu. Neither gives you the exact point that the mouse is at, but it really doesn't matter - all you want to do is pop up a menu on the selected nodes in the tree.
 
mira yang
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic