Search code examples
javafxrichtextfx

Get character index on right click for CodeArea


I am using CodeArea from RichTextFX in my code editor. I need to show context menu on right click and fill it based on the text where mouse is pointing. Like if I right click a function call, there will be "Go to declaration" in the context menu. To do that I need to get character index where the mouse is pointing. For MouseOverTextEvent there is the getCharacterIndex() method, but there is no such method in ContextMenuEvent or MouseEvent.

Saving the last index from MouseOverTextEvent is not a good option for me because I use MouseOverTextEvent for showing popup messages and this event has a 1 second delay (used setMouseOverTextDelay).

How can I get the character index for this right click event?


Solution

  • The hit(...) method inherited from GenericStyledArea gives you access to the index of the closest character, given the x and y coordinates in the code area's coordinate system (which you can readily get from the mouse event).