鉴于带有一些子节点的巨大AnchorPane
是ScrollPane
的内容,我如何滚动以使其中一个子节点在当前视口之外可见?
答案 0 :(得分:14)
您需要在content
内找到此内部节点的坐标,并相应地调整ScrollPane
的{{1}}和vValue
。
请参阅下一个小应用中的hValue
方法:
ensureVisible()
答案 1 :(得分:8)
我为这个案例创建了一个稍微优雅的版本。但是,我只能在y轴上进行测试。希望它有所帮助
private static void ensureVisible(ScrollPane pane, Node node) {
Bounds viewport = scrollPane.getViewportBounds();
double contentHeight = scrollPane.getContent().localToScene(scrollPane.getContent().getBoundsInLocal()).getHeight();
double nodeMinY = node.localToScene(node.getBoundsInLocal()).getMinY();
double nodeMaxY = node.localToScene(node.getBoundsInLocal()).getMaxY();
double vValueDelta = 0;
double vValueCurrent = scrollPane.getVvalue();
if (nodeMaxY < 0) {
// currently located above (remember, top left is (0,0))
vValueDelta = (nodeMinY - viewport.getHeight()) / contentHeight;
} else if (nodeMinY > viewport.getHeight()) {
// currently located below
vValueDelta = (nodeMinY + viewport.getHeight()) / contentHeight;
}
scrollPane.setVvalue(vValueCurrent + vValueDelta);
}
答案 2 :(得分:2)
要获得完美的快照,您需要考虑滚动条的开始和停止位置。 视口从0(Bounds scollpane)的一半视口开始,从max Bound停止一半的视口。 bounds vs viewport picture
如此: 半视口下的节点Y-> setVvalue设置为0 maxheigh-half视口上方的节点Y->将值设置为1
,在这之间,我们需要计算总高度-1viewport(一半起点在end的一半)和节点y-half viewport。 然后(节点y-半视口)/(总高度-1视口)
double heightViewPort = scrollPane.getViewportBounds().getHeight();
double heightScrollPane = scrollPane.getContent().getBoundsInLocal().getHeight();
double y = Label.getBoundsInParent().getMaxY();
if (y<(heightViewPort/2)){
scrollPane.setVvalue(0);
// below 0 of scrollpane
}else if ((y>=(heightViewPort/2))&(y<=(heightScrollPane-heightViewPort/2))){
// between 0 and 1 of scrollpane
scrollPane.setVvalue((y-(heightViewPort/2))/(heightScrollPane-heightViewPort));
}
else if(y>= (heightScrollPane-(heightViewPort/2))){
// above 1 of scrollpane
scrollPane.setVvalue(1);
}