Skip to content

StyledTextArea scrolls to the caret every time span style is changed #101

@mgrabmair

Description

@mgrabmair

I have a StyleClassedTextArea and load a String into it. I have a button event which calls setStyleClass a number of times using data external to the document. For some reason, when I call it, the text area always automatically scrolls to the bottom of the document. I have tried all kinds of variations, but it happens. Also, when I make the setStyleClass dependent on the document's content changing and I edit the text in the middle of the text field, it scroll-jumps into a position where the line I am editing becomes the bottom-most line in the view. What am I doing wrong?

Thanks,

Matthias


Minimal Code Example (just paste a lot of text into the field and click the button and provide some stylesheet in the css file defining a 'red' style):


package application;

//imports ...

public class Main extends Application {

private Scene scene;
private StyleClassedTextArea textArea;

@Override
public void start(Stage stage) {

    stage.setTitle("ScrollTest);

    try {
        BorderPane root = new BorderPane();
        textArea = new StyleClassedTextArea();
        textArea.setWrapText(true);

        BorderPane border = new BorderPane();
        HBox topBox = new HBox();

        Button updateButton = new Button("foo");            
        updateButton.setPrefSize(100, 20);
        updateButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override public void handle(ActionEvent e) {
                textArea.setStyleClass(5, 10, "red");
            }
        });


        topBox.getChildren().addAll(updateButton);

        scene = new Scene(border, 600, 400);
        border.setCenter(textArea);

        border.setTop(topBox);
        scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());

        stage.setScene(scene);
        stage.show();

    } catch(Exception e) {
        e.printStackTrace();
    }
}

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions