LogicalPosition () { JTextComponent textComponent = getTextComponent(); int caretPos = textComponent.getCaretPosition(); int line; int lineStart; if (textComponent instanceof JTextArea textArea) { try { line = textArea.getLineOfOffset(caretPos); lineStart = textArea.getLineStartOffset(line); } catch (BadLocationException e) { throw new RuntimeException(e); } } else { line = 0; lineStart = 0; } return new LogicalPosition(line, caretPos - lineStart); }