Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set the unit increment for a scroll pane in JavaFX?

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?

like image 450
Michael Berry Avatar asked Jul 16 '13 21:07

Michael Berry


People also ask

What is the difference between JScrollPane and JScrollBar?

A JScrollBar is a component and it doesn't handle its own events whereas a JScrollPane is a Container and it handles its own events and performs its own scrolling.

How do I add a vertical scroll bar in JavaFX?

By default, the scroll bar is oriented horizontally. However, you can set the vertical orientation by using the setOrientation method. The user can click the left or right button (down or up button for the vertical orientation) to scroll by a unit increment.

What is a scroll pane JavaFX?

The ScrollPane allows the application to set the current, minimum, and maximum values for positioning the contents in the horizontal and vertical directions. These values are mapped proportionally onto the layoutBounds of the contained node.


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

like image 64
zhujik Avatar answered Sep 25 '22 14:09

zhujik