FXMisc / RichTextFX

Rich-text area for JavaFX
BSD 2-Clause "Simplified" License
1.21k stars 237 forks source link

How to select text and move caret to the beginning of the selection? #1239

Closed PavelTurk closed 3 months ago

PavelTurk commented 3 months ago

I need to select text and move caret to the beginning of the selection. This is my code:

public class JavaFxTest7 extends Application {

    private final CodeArea codeArea = new CodeArea();

    @Override
    public void start(Stage stage) {
        this.codeArea.appendText("Some text is here.");
        VBox.setVgrow(codeArea, Priority.ALWAYS);
        codeArea.selectRange(0, 4);
        codeArea.moveTo(0);
        codeArea.requestFollowCaret();
        Scene scene = new Scene(new VBox(codeArea), 600, 200);
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch();
    }
}

I want to select from 0 to 4 and move caret to 0. However, it doesn't work. At the same time, I don't have any problems when I need to move caret to selection end. Could anyone say how to do it?

Jugen commented 3 months ago

Just reverse the selection coordinates: codeArea.selectRange( 4, 0 );

PavelTurk commented 3 months ago

@Jugen Yes, it worked. Thanks a lot!