如何调整ScrollPane以使其中一个子节点在视口中可见?

时间:2016-11-22 20:44:31

标签: javafx java-8 javafx-8

我想弄清楚如何滚动errno_t strcpy_s( char *strDestination, size_t numberOfElements, const char *strSource ); ,以便嵌套在其内容中的任何ScrollPane都可见。目标Node可能有许多我无法预测的嵌套级别。

这与我能够得到的一样接近。它可以工作,但它是一个非常黑客,并有一个错误,在某些条件下产生无限的递归调用循环。必须有更好的方法。

Node

1 个答案:

答案 0 :(得分:2)

只需将坐标从Node的坐标系转换为内容的坐标系。根据内容大小,视口大小和变换后的坐标,您可以确定滚动位置:

public static void scrollTo(ScrollPane scrollPane, Node node) {
    final Node content = scrollPane.getContent();
    Bounds localBounds = node.getBoundsInLocal();
    Point2D position = new Point2D(localBounds.getMinX(), localBounds.getMinY());

    // transform to content coordinates
    while (node != content) {
        position = node.localToParent(position);
        node = node.getParent();
    }

    final Bounds viewportBounds = scrollPane.getViewportBounds();
    final Bounds contentBounds = content.getBoundsInLocal();

    scrollPane.setHvalue(position.getX() / (contentBounds.getWidth() - viewportBounds.getWidth()));
    scrollPane.setVvalue(position.getY() / (contentBounds.getHeight() - viewportBounds.getHeight()));
}
相关问题