JavaFX automated Scroll

81 views Asked by At

I have a gridpane in a scrollpane. One gridcell has the size of the scrollpane so you have to scroll to see the other gridcells. Now I want to scroll automatically to a explicit grid cell.

Does anyone have an idea how I can do that?

1

There are 1 answers

0
James_D On

Assuming all the grid cells are the same size (which is implied by "one gridcell has the size of the scrollpane"), and you know how many rows and columns there are in the grid pane, you should be able to do something like this:

private ScrollPane scrollPane = ... ;
private int numColumns = ... ;
private int numRows = ... ;

// these are defaults, but just to be sure:
scrollPane.setHmin(0.0);
scrollPane.setHmax(1.0);
scrollPane.setVmin(0.0);
scrollPane.setVmax(1.0);


private void scrollTo(int column, int row) {
    scrollPane.setHvalue((1.0 * column) / (numColumns - 1));
    scrollPane.setVvalue((1.0 * row) / (numRows - 1));
}

That may need a bit of adjustment if you have borders etc.