JavaFX TextArea滚动条移动事件

JavaFX TextArea scrollbar move event

我需要将 "scrollbar move" 侦听器添加到 TextArea

但是当我添加

textArea.addEventFilter(ScrollEvent.ANY, (x) -> System.out.println(textArea.getScrollTop()));

它只监听使用鼠标滚轮触发的事件——鼠标滚轮滚动。

当我用鼠标选中滚动条并上下拖动时,没有捕获任何事件。

我尝试过不同的方法

textArea.addEventFilter(ActionEvent.ANY, (x) -> System.out.println(textArea.getScrollTop())); 
textArea.setOnScroll(...);
textArea.setOnScrollStarted(...);
textArea.setOnScrollFinished(...);
textArea.textProperty().addListener((observable, oldValue, newValue) -> {
   System.out.println("> " + textArea.getScrollTop());
});

使用滚动条滚动没有任何响应。

我怎样才能捕捉到这样的事件?

您可以使用 scrollLeftProperty 属性

The number of pixels by which the content is horizontally scrolled.

scrollTopProperty 属性

The number of pixels by which the content is vertically scrolled.

TextArea 个待听:

TextArea ta = new TextArea();
ta.scrollTopProperty().addListener((obs, oldVal, newVal) -> 
    System.out.println("Position from top: " + newVal);

ta.scrollLeftProperty().addListener((obs, oldVal, newVal) -> 
    System.out.println("Position from left: " + newVal));

示例输出:

Position from top: 36.0
Position from left: 16.6046511627907