JavaFX TextArea和autoscroll [英] JavaFX TextArea and autoscroll

查看:120
本文介绍了JavaFX TextArea和autoscroll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让TextArea使用通过事件处理程序放入的新文本自动滚动到底部。每个新条目只是一个长字符串,每个条目用换行符分隔。我已经尝试了一个更改处理程序,它将setscrolltop设置为Double.MIN_VALUE,但无济于事。关于如何做到这一点的任何想法?

I am trying to get a TextArea to autoscroll to the bottom with new text which is put in via an event handler. Each new entry is just one long string of text with each entry separated by a line break. I have tried a change handler which sets setscrolltop to Double.MIN_VALUE but to no avail. Any ideas of how this could be done?

推荐答案

你必须在 TextArea <中添加一个监听器/ code>元素在值改变时滚动到底部:

You have to add a listener to the TextArea element to scroll to the bottom when it's value is changed:

@FXML private TextArea txa; 

...

txa.textProperty().addListener(new ChangeListener<Object>() {
    @Override
    public void changed(ObservableValue<?> observable, Object oldValue,
            Object newValue) {
        txa.setScrollTop(Double.MAX_VALUE); //this will scroll to the bottom
        //use Double.MIN_VALUE to scroll to the top
    }
});

但是当你使用 setText(text)<时,不会触发此监听器/ code>方法,所以如果你想在 setText(text)之后触发它,请使用 appendText(text)紧随其后:

But this listener is not triggered when you use the setText(text) method, so if you want to trigger it after a setText(text) use the appendText(text) right after it:

txa.setText("Text into the textArea"); //does not trigger the listener
txa.appendText("");  //this will trigger the listener and will scroll the
                     //TextArea to the bottom

一旦 setText()触发已更改的侦听器,这听起来更像是一个错误,但事实并非如此。这是我自己使用的解决方法,希望它可以帮助你。

This sounds more like a bug, once the setText() should trigger the changed listener, however it doesn't. This is the workaround I use myself and hope it helps you.

这篇关于JavaFX TextArea和autoscroll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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