且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在javaFx java8中禁用textArea自动滚动

更新时间:2022-05-16 07:07:14

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

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