Search code examples
javajavafxappendtextareamvvmfx

Javafx mvvmfx textarea append


I'm having an issue with my JavaFX application. Problem is that I can't use append function, only setText. Why this is a problem is because auto scroll is not working without append. What is the way to update or auto-scroll my TextArea each time new text appears?


Solution

  • JavaFX component TextArea inherits the method setText(String value) from TextInputControl and its documentations sais:

    Sets the value of the property text.

    which means the text is set as a new one. To just insert a new line (update, append) the text, you have to use the method appendText(String value) inherited as well.

    Appends a sequence of characters to the content.

    Both of the input String value's must be not null.

    To scroll to the end, you have to implement a listener which is triggered by any text change (setText(), appendText()) and perform the scroll using the method setScrollTop(double value). The parameter double value is the number of pixels by which the content is vertically scrolled - using the largest possible double value Double.MAX_VALUE makes it scroll to the end.

    textArea.textProperty().addListener((observable, oldValue, newValue) ->  
        textArea.setScrollTop(Double.MAX_VALUE);
    });