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


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 similar method for JavaFX.

I saw that the Parent class is what I want to work with since it is the class from which all Node classes that bear children are derived.

This is what I have so far (and I haven't really found anything on google with searches like "JavaFX get all nodes from a scene"):

public static ArrayList<Node> GetAllNodes(Parent root){
    ArrayList<Node> Descendents = new ArrayList<>();
    root.getChildrenUnmodifiable().stream().forEach(N -> {
        if (!Descendents.contains(N)) Descendents.add(N);
        if (N.getClass() == Parent.class) Descendents.addAll(

So how do I tell if N is a parent (or extended from a parent)? Am I doing that right? It doesn't seem to be working... It's grabbing all the nodes from the root (parent) node but not from the nodes with children in them. I feel like this is something that's probably got an answer to it but I'm just asking the question... wrong. How do I go about doing this?

4/11/2019 10:41:58 PM

Accepted Answer

public static ArrayList<Node> getAllNodes(Parent root) {
    ArrayList<Node> nodes = new ArrayList<Node>();
    addAllDescendents(root, nodes);
    return nodes;

private static void addAllDescendents(Parent parent, ArrayList<Node> nodes) {
    for (Node node : parent.getChildrenUnmodifiable()) {
        if (node instanceof Parent)
            addAllDescendents((Parent)node, nodes);
7/27/2014 11:57:02 PM

I use this,

public class NodeUtils {

    public static <T extends Pane> List<Node> paneNodes(T parent) {
        return paneNodes(parent, new ArrayList<Node>());

    private static <T extends Pane> List<Node> paneNodes(T parent, List<Node> nodes) {
        for (Node node : parent.getChildren()) {
            if (node instanceof Pane) {
                paneNodes((Pane) node, nodes);
            } else {

        return nodes;


List<Node> nodes = NodeUtils.paneNodes(aVBoxOrAnotherContainer);

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

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow