jmt
jmt

Reputation: 751

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 ?

Upvotes: 1

Views: 908

Answers (1)

DVarga
DVarga

Reputation: 21829

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

Upvotes: 3

Related Questions