JavaFX:在textArea中设置文本后,在单独的线程中设置滚动到底部不起作用 [英] JavaFX : After setting text in textArea, setting scroll to bottom in separate thread is not working

查看:810
本文介绍了JavaFX:在textArea中设置文本后,在单独的线程中设置滚动到底部不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个JavaFX应用程序,我用一个后台进程更新日志。所以我在TextArea中设置日志文本,并使用 logs.setScrollTop(Double.MAX_VALUE)设置滚动到底部。但滚动条从底部设置为一点点。
我还在ScrollPan中尝试了TextFlow,并使用 logDisplay.setVvalue(1.0)将滚动设置为底部。它也给出了相同的结果。

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.

推荐答案

我不知道这个问题的实际问题,但我找到了另一种解决方案。

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());

这对我有用。:)

这篇关于JavaFX:在textArea中设置文本后,在单独的线程中设置滚动到底部不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆