JavaFX : After setting text in textArea, setting s

2019-06-18 15:43发布

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.

1条回答
对你真心纯属浪费
2楼-- · 2019-06-18 16:13

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. :)

查看更多
登录 后发表回答