How to get LocationOnScreen of Editor's cursor

Hi guys,
I need to popup custom component and place it's upper left corner on cursor position in Editor screen, but I can't find a simple way to get cursor's location on screen. Do I have to calculate it by myself? I have upper left corner location of editor, line # and column # and font attributes,so, I guess I can calculate it, but is there better way to get this data? Zaranee blagodaren...

Comment actions Permalink

Hi Andrei,



Comment actions Permalink

Hi Denis,

Thanks a lot for so quick response.
It gives location outside of screen (e.g. location = java.awt.Point[x=35,y=2516]). I understand that it's location relatively to the top left corner of entire file (not only it's visible part).
This is what I'm doing additionally to your method:

                final Point cursorAbsoluteLocation = editor.visualPositionToXY(caretModel.getVisualPosition());
                final Point editorLocation= editor.getComponent().getLocationOnScreen();
                final Point editorContentLocation = editor.getContentComponent().getLocationOnScreen();
                final Point popupLocation = new Point(editorContentLocation.x + cursorAbsoluteLocation.x ,
                                                                        editorLocation.y + cursorAbsoluteLocation.y - editor.getScrollingModel().getVerticalScrollOffset());

For x I'm using editorContentLocation instead of editorLocation, because it's include left margin.
It's working, but is it right way to do it?

Comment actions Permalink

That's correct calculation of the caret location relative to screen coordinates.



Please sign in to leave a comment.