12

いくつかのサブノードを持つ巨大なAnchorPaneものがのコンテンツであるScrollPaneとすると、現在のビューポートの外側にあるサブノードの1つを表示するためにスクロールするにはどうすればよいですか?

4

3 に答える 3

16

編集: 次の例のコードは、私のコードよりも正確です: https://stackoverflow.com/a/23518314/1054140

内部のこの内部ノードの座標を見つけて、それに応じて を調整する必要がありcontentます。ScrollPanevValuehValue

ensureVisible()次の小さなアプリのメソッドを参照してください。

public class ScrollPaneEnsureVisible extends Application {

    private static final Random random = new Random();

    private static void ensureVisible(ScrollPane pane, Node node) {
        double width = pane.getContent().getBoundsInLocal().getWidth();
        double height = pane.getContent().getBoundsInLocal().getHeight();

        double x = node.getBoundsInParent().getMaxX();
        double y = node.getBoundsInParent().getMaxY();

        // scrolling values range from 0 to 1
        pane.setVvalue(y/height);
        pane.setHvalue(x/width);

        // just for usability
        node.requestFocus();
    }

    @Override
    public void start(Stage primaryStage) {

        final ScrollPane root = new ScrollPane();
        final Pane content = new Pane();
        root.setContent(content);

        // put 10 buttons at random places with same handler
        final EventHandler<ActionEvent> handler = new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                int index = random.nextInt(10);
                System.out.println("Moving to button " + index);
                ensureVisible(root, content.getChildren().get(index));
            }
        };

        for (int i = 0; i < 10; i++) {
            Button btn = new Button("next " + i);
            btn.setOnAction(handler);
            content.getChildren().add(btn);
            btn.relocate(2000 * random.nextDouble(), 2000 * random.nextDouble());
        }

        Scene scene = new Scene(root, 300, 250);
        primaryStage.setScene(scene);
        primaryStage.show();

        // run once to don't search for a first button manually
        handler.handle(null);
    }

    public static void main(String[] args) { launch(); }
}
于 2012-10-11T13:20:34.060 に答える
10

この場合のために、もう少しエレガントなバージョンを作成しました。ただし、y軸でしかテストできませんでした。それが役に立てば幸い

private static void ensureVisible(ScrollPane scrollPane, 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);
}
于 2016-03-03T20:48:07.967 に答える