简体   繁体   中英

JavaFX TextArea scrollbar move event

I need to add "scrollbar move" listener to a TextArea .

But when I added

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

It is only listening to events triggered by using mousewheel - mousewheel scroll.

When I pick scrollbar by mouse a drag it up and down, no event is caught.

I've tried different approach

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

Nothing is responding to scrolling using scrollbar.

How can I catch such an event ?

You can use the scrollLeftProperty property

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

and the scrollTopProperty property

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

of the TextArea to be listened:

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

Sample output:

Position from top: 36.0
Position from left: 16.6046511627907

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM