2012-10-22 14 views
7

Jak mogę przełączać sterowanie sceny? Próbowałem z getChildrenUnmodifiable(), ale zwraca tylko pierwszy poziom dzieci.Pętla JavaFX nad kontrolkami scenegraph

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()); 
     } 
    } 
} 

Odpowiedz

8

Należy skanować rekurencyjnie. Na przykład:

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); 
     } 
    } 
} 
3

Oto zmodyfikowana wersja amru's answer że używam, metoda ta daje elementy określonego typu:

private <T> List<T> getNodesOfType(Pane parent, Class<T> type) { 
    List<T> elements = new ArrayList<>(); 
    for (Node node : parent.getChildren()) { 
     if (node instanceof Pane) { 
      elements.addAll(getNodesOfType((Pane) node, type)); 
     } else if (type.isAssignableFrom(node.getClass())) { 
      //noinspection unchecked 
      elements.add((T) node); 
     } 
    } 
    return Collections.unmodifiableList(elements); 
} 

Aby uzyskać wszystkie składniki:

List<Node> nodes = getNodesOfType(pane, Node.class); 

Aby uzyskać tylko przyciski:

List<Button> buttons= getNodesOfType(pane, Button.class); 
Powiązane problemy