javajavafxscrollpane

JavaFX - How to programmatically scroll a ScrollPane upon button click?


I wanted to know if there was any "easy" way I can set the vertical scroll-bar to be "scrolled" a specific amount upon a button press, or really upon any event at all.

For example, I have an application which has a side bar and one of the options on the sidebar is Settings. Settings has sub items, and I wanted to add the functionality to click on one of the sub items and it will open the Settings page and automatically scroll down to that specific point on the page where that sub item is located.


Solution

  • To programmatically scroll a ScrollPane vertically simply manipulate the vvalue property. Here's the documentation of that property:

    The current vertical scroll position of the ScrollPane. This value may be set by the application to scroll the view programatically. The ScrollPane will update this value whenever the viewport is scrolled or panned by the user. This value must always be within the range of vmin to vmax. When vvalue equals vmin, the contained node is positioned so that its layoutBounds minY is visible. When vvalue equals vmax, the contained node is positioned so that its layoutBounds maxY is visible. When vvalue is between vmin and vmax, the contained node is positioned proportionally between layoutBounds minY and layoutBounds maxY.