Reputation: 51229
Is it possible to scroll ScrollPane
programmatically to given position?
ScrollPane#vvalue
and ScrollPane#hvalue
are useless, because they vary from 0 to 1. Are there any absolute properties or I should code them myself?
Upvotes: 1
Views: 3140
Reputation: 1203
@trilogy asked for a code answer to this question... And here it is:
static void scrollToPosition(ScrollPane scrollPane, double topLeftX, double topLeftY) {
Bounds contentBounds = scrollPane.getContent().getBoundsInLocal();
Bounds viewportBounds = scrollPane.getViewportBounds();
double hValue = topLeftX / (contentBounds.getWidth() - viewportBounds.getWidth());
double vValue = topLeftY / (contentBounds.getHeight() - viewportBounds.getHeight());
scrollPane.setHvalue(hValue);
scrollPane.setVvalue(vValue);
}
Upvotes: 0
Reputation: 102
Use setHvalue
and setVvalue
to position your scrollPane.
Values can be calculated with respect to Nodes
For eg:
double nodeX = dummyNode.getBoundsInParent().getMaxX();
double nodeY = dummyNode.getBoundsInParent().getMaxY();
double scrollPaneWidth = dummyScrollPane.getContent().getBoundsInLocal().getWidth()
double scrollPaneHeight = dummyScrollPane.getContent().getBoundsInLocal().getHeight()
dummyScrollPane.setVvalue(nodeY/height);
dummyScrollPane.setHvalue(nodeX/width);
Upvotes: 0
Reputation: 82491
Those properties are not useless, you just need to know how to use them correctly. (There are no methods allowing you to specify absolute values directly.)
Assuming the ScrollBar
is visible, you know the following equation
topLeft / (contentSize - viewportSize) = scrollValue / scrollMax
This can be reformulated as
scrollValue = (topLeft * scrollMax) / (contentSize - viewportSize)
= topLeft / (contentSize - viewportSize)
hvalue = topLeftX / (contentWidth - viewportWidth)
vvalue = topLeftY / (contentHeight - viewportHeight)
Where topLeftX
and topLeftY
are the x and y coordiantes of the pixel in the top left corner of the viewport. Use the viewportBounds
to determine the size of the viewport.
Upvotes: 4
Reputation: 927
If you mean move the scroll pane a given number of pixels then get the image size and calculate how much you need to move with hvalue
or vvalue
.
int pixelsH = 45;
int change = pixelsH/scrollpane.getWidth();
scrollpane.setHvalue(scrollpane.getHValue() + change);
Same concept for vertical change.
Upvotes: 1