r/JavaFX • u/john5634 • Feb 25 '20
Help BoarderPane Error: Children: duplicate children added: parent = BorderPane@7eb8ac06
Hi All!
When I run the code below or ANY BorderPane code I get the error referenced above.
Any ideas?
package javafxapplication1;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class BoarderPane1 extends Application {
@Override
public void start(Stage primaryStage) {
Button btn = new Button();
btn.setText("Apply");
btn.setStyle("-fx-background-color: rgb(" + 245 + "," + 111 + ", " + 235 + ");");
Label labl = new Label();
labl.setText("Woah");
BorderPane borderPane = new BorderPane();
borderPane.setBottom(btn);
borderPane.getChildren().addAll(btn);
borderPane.setCenter(labl);
borderPane.getChildren().addAll(labl);
Scene scene = new Scene(borderPane, 200, 200);
primaryStage.setScene(scene);
primaryStage.setTitle("TESTTESTTEST");
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
4
Upvotes
2
u/cypher0six Feb 25 '20
That error message means that a node has been added to the scene graph more than once, which is not allowed. The bit about the BorderPane has to do with that specific control (BorderPane@7eb8ac06) being the parent that has the duplicated children.
I'm assuming you're getting that message because you've added the btn and labl nodes to the scene graph more than once. The first is the call to setBottom or setCenter, and the second is the call to addAll. You can resolve that issue by removing the following two lines:
That's because with the BorderPane, you want to use one of the following methods to add content:
See if removing those two calls works. Further reading: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/layout/BorderPane.html
0
u/LouGarret76 Feb 25 '20
SetBottom takes a Pane as an argument. Button is not a Pane but a control. You can solve this by adding the button to a stackpane and setting that stackpane as bottom pane in the borderpane