Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX : After setting text in textArea, setting scroll to bottom in separate thread is not working

I created one JavaFX application where I'm updating log with one background process. So I'm setting log text in TextArea and setting scroll to bottom using logs.setScrollTop(Double.MAX_VALUE). but scrollbar is set to little bit up from the bottom. I also tried TextFlow inside ScrollPan and setting scroll to bottom using logDisplay.setVvalue(1.0). It is also giving the same result.

Platform.runLater(() -> {
    logs.setText([setting log text]);//TextArea logs
    logs.setScrollTop(Double.MAX_VALUE));
});

//For TextFlow inside ScrollPane
Platform.runLater(() -> {
    logs.setText([setting log text]);//Text logs
    logDisplay.setVvalue(1.0);
});

I also tried to run code in separate thread like

new Thread() {
    public void run(){
        System.out.println("called set test");
        logs.setText([setting log text]);//Text logs
        logDisplay.setVvalue(1.0);
    }
}.start();

But nothing is working :(
Can you help me what's wrong in this?
Thanks

--Edit--
Looks like the problem is because of threading issue. Scrollbar value is updating to the previous text value. Actually while retrieving scroll value it's not retrieving latest value but it's getting older value so scrollbar set to end of the previous message, not actual last line.

like image 385
Piyush Avatar asked Apr 27 '15 12:04

Piyush


1 Answers

I don't know the actual problem of this issue, but I found an alternative solution.

I'm setting caret's position at end of text using length of text.

logs.setText(logText);
logs.positionCaret(logText.length());

It is working for me. :)

like image 63
Piyush Avatar answered Sep 25 '22 14:09

Piyush