如何在javaFx java8中禁用textArea自动滚动 [英] How to disable textArea auto-scrolling in javaFx java8

查看:241
本文介绍了如何在javaFx java8中禁用textArea自动滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序可以将大量文本输出到我在我的应用程序中用作控制台的文本区域。

I have a program that prints out loads of text to the text area which i used as a console in my app.

它输出实时建筑信息。

默认情况下它会向下滚动到底部,这很不错,但我希望能够抓住滚动条并在文本区域中查看正在进行的操作文本仍然附加到文本区域。

By default it scrolls down to the bottom which is nice, but i would like to be able to grab the scrolling bar and go up in text area to check what is going on while the text is still appending to the text area.

我的问题是当我抓住滚动条时我如何禁用自动滚动?

My question is how i can disable auto-scrolling when i grab the scrolling bar ?

理想情况是:
如果滚动条回到最后,则自动滚动
当我将滚动条向上调整为难以阅读时禁用自动滚动文本一直跳到最后的任何内容。

The ideal scenario would be : Auto-scroll if the scrolling bar is way back to the end Disable auto-scrolling when i took the scrolling bar up as its difficult to read anything while the text jump back to the end all the time.

这是我的文本区域appender

This is my text area appender

private void run(final TextArea console) {
    try {
        console.clear();
        BufferedReader stdInput;
        stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));

        String line;
        while ((line = stdInput.readLine()) != null) {
            String feed = line;
            Platform.runLater(() -> console.appendText(feed + "\n"));
        }
    } catch (Exception e) {
        console.appendText(e.toString());
    }
}


推荐答案

我有同样的问题,这解决了它。基本上在文本更新之前/之后获取滚动位置并确保它是相同的:

I had the same problem, and this solved it. Basically taking the scroll position before/after the text update and making sure it's the same:

Platform.runLater(() -> {
    double pos = console.getScrollTop();
    int anchor = console.getAnchor();
    int caret = console.getCaretPosition();
    console.appendText(feed + '\n');
    console.setScrollTop(pos);
    console.selectRange(anchor, caret);
});

注意:这是用.setText()测试的,但不是用appendText()测试的

Note: This is tested with .setText(), but not with appendText()

编辑:添加选择代码 - 没有它,它将有未定义的行为

Added selection code - without it, it will have undefined behavior

这篇关于如何在javaFx java8中禁用textArea自动滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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