In javaFx, why do we need to call "getChildren ()" when we add an item to the window?

For example, when we add a new button to the panel, we need to write the following code:

 StackPane pane = new StackPane();
 pane.getChildren().add(new Button("OK"));

Why do we need to call "getChildren ()?" what is he doing Why can't we say:

 StackPane pane = new StackPane();
 pane.add(new Button("OK"));

We add a button to the panel. We don’t add it to our children,

+4
source share
3 answers

- " , , API". , , , , API . , . , , - API.

, . ( , , - - , .) , API , , , .


. - ( JavaFX), . , , .., , , , . , , .. - .

, . node, . .. , "", ( , ). , , , . , . node ( node) "" (, ): Node ( "node" - ) getParent(), node (, node). , UI (), , getChildren(), , node. Oracle JavaFX , , .

, , , getChildren(), , , , . "node" , "parent" "child" . , getChildren() , ( - StackPane ).


API Pane (, StackPane)

API, , , , , StackPane. , ( "node" ) StackPane, add(...), Node. , :

  • node StackPane
  • StackPane
  • StackPane
  • () StackPane
  • ( z-, .. , , ), node ( -, - ).
  • node "", , .
  • ,

StackPane , .

StackPane, ( ), . , , . StackPane ( , Pane) , , . , , List 1 List - , Java.

, StackPane, , . - ( , , , ),

public class StackPane {

    private final List<Node> children = new ArrayList<>(); // or some other list implementation...

    public void add(Node node) {
        children.add(node);
    }

    public boolean remove(Node node) {
        return children.remove(node);
    }

    public void add(int index, Node node) {
        children.add(index, node);
    }

    public boolean remove(int index) {
        return children.remove(index);
    }

    public void addAll(Collection<Node> nodes) {
        children.addAll(nodes);
    }

    // lots more methods like this...

    // lots of layout code omitted...
}

, . ; , . , API, ​​ StackPane, 2:

public class StackPane {
    private final List<Node> children = new ArrayList<>();

    public List<Node> getChildren() {
        return children ;
    }

    // layout code omitted...
}

, API , , , List, , , . JavaFX, , () Java, API List , , . , ( , ):

StackPane pane = new StackPane();

Button button = new Button("OK");

pane.getChildren()
    // oooh, a List, I know how those work
    .add(button);

List<Label> lotsOfLabels = Arrays.asList(new Label("One"), new Label("Two"), new Label("Three"));

pane.getChildren()
    // still a list, I know more things I can do here:
    .addAll(lotsOfLabels);

pane.getChildren().remove(button);

pane.getChildren().clear();

, API JavaFX , , , .

, List , API StackPane, StackPane , Java-.


  • , Pane , List: , , ( , ). , JavaFX List, ObservableList, List "" .
  • , JavaFX : , List ? List , , , , API, , .
+4

, add(), addAll(), remove(), removeAll() ..

, , List<E>, , . javafx.scene.layout.Pane ObservableList<Node>, List<E>

+3

The panel tracks an array of its children. The reason you cannot just call pane.add(new Button("OK"));is because the StackPane class simply does not provide this method. You must add the element directly to the panel's child array.

0
source

Source: https://habr.com/ru/post/1668566/


All Articles