The ScrollBar class in JavaFX contains a property for setting the unit increment, which is what I'm after - however I can't find how to get at this ScrollBar, or set the unit increment some other way from the ScrollPane class! I presume I must be missing something obvious - how do I achieve this?
3 Answers
you can setup a ScrollEventListener to the ScrollPane and thus override the original behavior. This way, for example, I implemented a ScrollPane that scrolls horizontally instead of vertically. This is what the relevant part of my code looks like:
public class Overview extends ScrollPane {
...
  private void setupHorizontalScrolling() {
    this.setOnScroll(new EventHandler<ScrollEvent>() {
      @Override
      public void handle(ScrollEvent scrollEvent) {
        double deltaY = scrollEvent.getDeltaY()*2; // *2 to make the scrolling a bit faster
        double width = Overview.this.getContent().getBoundsInLocal().getWidth();
        double hvalue = Overview.this.getHvalue();
        Overview.this.setHvalue(hvalue + -deltaY/width); // deltaY/width to make the scrolling equally fast regardless of the actual width of the component
      }
    });
  }
...
}
To meet your requirement, you can just change the line where the get/setHvalue is called to get/setVvalue and then you can adjust the scrolling like you want.
- 6,514
 - 2
 - 36
 - 43
 
Technically, you should be able to get to it using the .lookup("scrollbar") method after the skin is initialized (if the scrollbar is visible).  I don't like that answer though.
I don't think you're missing anything according to this bug ticket:
- How to access to ScrollBar component in ScrollPane control ?
https://bugs.openjdk.java.net/browse/JDK-8091864 
I'd encourage you to jump on that, present your use case, vote, and see if you can get some sort of response in the future.
- 6,910
 - 8
 - 54
 - 61
 
- 6,465
 - 14
 - 30
 
What I ended up using is:
  @FXML
  private ScrollPane scrollPane;
  @FXML
  public void initialize() {
    Platform.runLater(() -> setFasterScroller(scrollPane));
  }
  private static void setFasterScroller(ScrollPane scrollPane) {
    ScrollBar verticalScrollbar = (ScrollBar) scrollPane.lookup(".scroll-bar:vertical");
    double defaultUnitIncrement = verticalScrollbar.getUnitIncrement();
    verticalScrollbar.setUnitIncrement(defaultUnitIncrement * 3);
  }
This one modifies the speed of the vertical scroller, what you would want to do more often, but it could be changed to .scrollbar:horizontal as well. 3 is used as modifier to accelerate the movement.
- 6,294
 - 6
 - 27
 - 45
 
- 
                    I tried this and it has no effect on the `ScrollPane`. – vovahost Sep 14 '20 at 21:58
 - 
                    1`.lookup()` only looks up by element id. To search by other properties (CSS class, pseudoclass) you use `.lookupAll()`. – Sergey Kirienko Oct 19 '21 at 14:46