SteeveDroz
SteeveDroz

Reputation: 6136

Add a Node to a class that extends Scene

How can I add a custom pane to a custom scene in that code?

public class MainScene extends Scene {
  public MainScene() {
    super(new FlowPane(), 800, 600);
  }

  public void addItem(String name) {
    Item item = new Item(name); // Item extends Pane.
    getRoot().getChildren().add(item); // That obviously doesn't work.
  }
}

Upvotes: 0

Views: 879

Answers (1)

ItachiUchiha
ItachiUchiha

Reputation: 36742

In your example, you do not have a reference to the root element.

Without asking it from the user, you cannot pass the reference of the root element because of the use of super().

Since getRoot() returns a Parent, you cannot use getChildren() on it for obvious reasons.

What you can do is to type-cast the getRoot() to FlowPane.

public void addItem(String name) {
    Pane item = new Pane(); // Item extends Pane.
    ((FlowPane)getRoot()).getChildren().add(item);
}

Upvotes: 1

Related Questions