Search code examples
javafxrichtextfx

Get actual line number where curser is JavaFX with RichTextFX


Is there a function in RichTextFX to get the actual line number where the curser?

Github: RichTextFX


Solution

  • You can get the index of the caret ("cursor") with

    int offset = textArea.getCaretPosition();
    

    and then convert to a TwoDimensional.Position with

    Position pos = textArea.offsetToPosition(offset);
    

    From there, pos.getMajor() is the paragraph (line) number (and pos.getMinor() is the index of the caret within the line).