Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I change the amount by which ScrollPane scrolls?

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?

like image 816
usertest Avatar asked Sep 23 '15 12:09

usertest


People also ask

How do I make my JScrollPane scroll faster?

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

What is the difference between scrollbar and ScrollPane?

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.


2 Answers

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.

like image 85
Andreas Koch Avatar answered Sep 19 '22 12:09

Andreas Koch


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.

like image 21
James_D Avatar answered Sep 21 '22 12:09

James_D