I need to know how to change the amount that a ScrollPane
object use to move the content up and down. For example, a ScrollBar
have these two methods for that:
scrollBar.setUnitIncrement(10);
scrollBar.setBlockIncrement(50);
How do I do the same with a ScrollPane
instead of ScrollBar
?
Just use the reference to your JScrollPane object, get the vertical scroll bar from it using getVerticalScrollBar , and then call setUnitIncrement on it, like this: myJScrollPane. getVerticalScrollBar(). setUnitIncrement(16);
A Scrollbar is a Component, but not a Container. A ScrollPane is a Container. A ScrollPane handles its own events and performs its own scrolling.
Had the same question and was unable to find the answer. Finally got the correct approach from the link provided by Erik and adapted it to my needs.
As the scrollPane itself did not fire the "setOnScroll"-Event (possibly because it gets consumed before doing so), I had to set the EventHandler on the content of the ScrollPane:
scrollPane_Content.setContent(vBox_Content);
vBox_Content.setOnScroll(new EventHandler<ScrollEvent>() {
@Override
public void handle(ScrollEvent event) {
double deltaY = event.getDeltaY()*6; // *6 to make the scrolling a bit faster
double width = scrollPane_Content.getContent().getBoundsInLocal().getWidth();
double vvalue = scrollPane_Content.getVvalue();
scrollPane_Content.setVvalue(vvalue + -deltaY/width); // deltaY/width to make the scrolling equally fast regardless of the actual width of the component
}
});
Doing so works with using the scrollbar directly as well as using the mousewheel to scroll up and down.
Hope this helps others who are looking for speeding up scrolling on a scrollPane.
In an external style sheet you can do
.scroll-pane .scroll-bar:vertical {
-fx-unit-increment: 10 ;
-fx-block-increment: 50 ;
}
.scroll-pane .scroll-bar:horizontal {
-fx-unit-increment: 5 ;
-fx-block-increment: 20 ;
}
for example. If you want it to apply to just a single scroll pane, give the scroll pane an id, etc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With