Curious
Curious

Reputation: 109

How to disable down scrolling in ScrollPane in JavaFX?

How to disable only down scrolling in ScrollPane when it reaches a specific Vvalue?

Upvotes: 1

Views: 2007

Answers (2)

DVarga
DVarga

Reputation: 21799

You can set the vmaxProperty of the ScrollPane.

The maximum allowable vvalue for this ScrollPane. Default value is 1.

By setting this property the scrollbar of the ScrollPane is rescaled, so the bottom-most position of the scrollbar is actually the specified value.

This will allow only the 40% of the vertical space to be scrolled:

scrollPane.setVmax(0.4);

Another possiblity is to interrupt down-scrolling by listening to the vvalueProperty property of the ScrollPane.

With this approach, the scrollbar of the ScrollPane is stopped on a defined value, so the bottom-most position is still 100%.

final double maxDownScroll = 0.4;
scrollPane.vvalueProperty().addListener((obs, oldVal, newVal) -> {
    if ((double) newVal > maxDownScroll)
        scrollPane.setVvalue(maxDownScroll);
});

Note: Both approaches is more generic than adding a filter to the ScrollEvent of the ScrollPane as they work in case of moving the scrollbar and also in case of scrolling with the mouse (ScrollEvent is only fired when the mouse wheel, tack pad or similar device is used).

Upvotes: 2

Curious
Curious

Reputation: 109

OK I solved it. Code:

scrollPane.addEventFilter(ScrollEvent.SCROLL, new EventHandler<ScrollEvent>() {
                @Override
                public void handle(ScrollEvent event) {
                    //"0.4 is my the specific value"
                    if(scrollPane.getVvalue() > 0.4) {
                        if (event.getDeltaY() < 0) {
                            event.consume();
                        }
                    }
             }
       });

Upvotes: 2

Related Questions