Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a JTextPane and want to know if there is a way to get the (x,y) pixel position of the current cursor. I don't want the caret position that's from getCaretPosition(), but the pixel co-ordinates of where the cursor currently is.

I'm using a variable width font in the pane, so simply multiplying the cursor position by some fixed width won't work.

I need this because I want a JPopUpMenu to appear directly below where the cursor is, and the JPopUpMenu show() method only takes (x,y) pixel coordinates.

All help is appreciated.

Thanks in Advance.
Posted
Updated 31-Mar-11 4:56am
v2

1 solution

Hi,

You can translate the caret position:

Java
Rectangle caretRectangle = 
   myTextPane.getUI().modelToView(myTextPanepane, myTextPanepane.getCaretPosition());


The caretRectangle will then hold the (x, y) for the lower part and the upper part of the caret in pixels.

Hope this helps,
Fredrik
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900