简体   繁体   中英

How to disable down scrolling in ScrollPane in JavaFX?

如何在ScrollPane达到特定Vvalue时仅禁用向下滚动?

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

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

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