How do I get all nodes in a parent in JavaFX?

后端 未结 6 1558
耶瑟儿~
耶瑟儿~ 2020-12-29 09:58

In C# I found a method that was pretty sweet that allowed you to get all the descendants and all of THEIR descendants from a specified control.

I\'m looking for a si

6条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-29 10:13

    I use this,

    public class NodeUtils {
    
        public static  List paneNodes(T parent) {
            return paneNodes(parent, new ArrayList());
        }
    
        private static  List paneNodes(T parent, List nodes) {
            for (Node node : parent.getChildren()) {
                if (node instanceof Pane) {
                    paneNodes((Pane) node, nodes);
                } else {
                    nodes.add(node);
                }
            }
    
            return nodes;
        }
    }
    

    Usage,

    List nodes = NodeUtils.paneNodes(aVBoxOrAnotherContainer);
    

    This source code uses the references of the existing nodes. It does not clone them.

提交回复
热议问题