JavaFX循环遍历场景图形控件 [英] JavaFX looping over scenegraph controls

查看:87
本文介绍了JavaFX循环遍历场景图形控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何遍历场景的控件? 我尝试使用getChildrenUnmodifiable(),但它仅返回第一级子级.

How can I loop over the controls of the scene ? I tried with getChildrenUnmodifiable() but it returns only the first level of children.

public void rec(Node node){

    f(node);

    if (node instanceof Parent) {
        Iterator<Node> i = ((Parent) node).getChildrenUnmodifiable().iterator();

        while (i.hasNext()){
            this.rec(i.next());
        }
    }
}

推荐答案

您需要进行递归扫描.例如:

You need to scan recursively. For example:

private void scanInputControls(Pane parent) {
    for (Node component : parent.getChildren()) {
        if (component instanceof Pane) {
            //if the component is a container, scan its children
            scanInputControls((Pane) component);
        } else if (component instanceof IInputControl) {
            //if the component is an instance of IInputControl, add to list
            lstInputControl.add((IInputControl) component);
        }
    }
}

这篇关于JavaFX循环遍历场景图形控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆