Is there any way to get the cursor position of a TextArea?

We have a feature in our system that allows a user to insert boilerplate paragraphs (with some codes replaced with data) into notes - and the existing legacy version allows them to choose beginning of note, end of note, or cursor position. We’d certainly like to keep that capability in the CUBA version of the system but I’m not finding a way to get the cursor position of a TextArea.

Is that possible?

Hi,

To set cursor position the setCursorPosition method van be used. In order to get cursor position, I can suggest two options:

  1. Get cursor position from TextChangeEvent and store it somewhere by yourself, e.g.:
@Subscribe("textArea")
public void onTextAreaTextChange(TextInputField.TextChangeEvent event) {
    notifications.create()
            .withCaption("TextChangeEvent")
            .withDescription("Cursor Position: " + event.getCursorPosition())
            .show();
    lastKnownCursorPosition = event.getCursorPosition();
}
  1. Unwrap CUBA TextArea component to Vaadin TextArea component and use the getCursorPosition() method, e.g.:
@Subscribe("getPosBtn")
public void onGetPosBtnClick(Button.ClickEvent event) {
    notifications.create()
            .withCaption("Last Known Cursor Position: " +
                    textArea.unwrap(com.vaadin.ui.TextArea.class).getCursorPosition())
            .show();
}

Regards,
Gleb

1 Like

I implemented this approach and it works perfectly; thank you again!