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

后端 未结 6 1489
耶瑟儿~
耶瑟儿~ 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:18

    This works for me:

    public class FXUtil {
    
        public static final List getAllChildren(final Parent parent) {
            final List result = new LinkedList<>();
    
            if (parent != null) {
    
                final List childrenLvl1 = parent.getChildrenUnmodifiable();
                result.addAll(childrenLvl1);
    
                final List childrenLvl2 =
                        childrenLvl1.stream()
                                    .filter(c -> c instanceof Parent)
                                    .map(c -> (Parent) c)
                                    .map(FXUtil::getAllChildren)
                                    .flatMap(List::stream)
                                    .collect(Collectors.toList());
                result.addAll(childrenLvl2);
            }
    
            return result;
        }
    
    }
    

提交回复
热议问题