JavaFX - load FXML into FXML without include


I have a window created with FXML in the corresponding controller, I have a button, which loads a small box when a specific button is clicked. The box is designed using FXML as well.

When I load the box and want to add it into the window, I get this error:

javafx.fxml.LoadException: Root value already specified.
    at javafx.fxml.FXMLLoader.createElement(
    at javafx.fxml.FXMLLoader.processStartElement(
    at javafx.fxml.FXMLLoader.load(
    at javafx.fxml.FXMLLoader.load(
    at com.clientgui.DataPage.openStaticData(

And this is my code:

private void openStaticData(int dataObjectId, String titel)
        URL location = getClass().getResource("StaticDataBox.fxml");
        FXMLLoader loader = new FXMLLoader();
        loader.setBuilderFactory(new JavaFXBuilderFactory());
        final Region page = (Region) loader.load();  //line 79

        StaticDataBox staticDataBox = (StaticDataBox) loader.getController();

        Platform.runLater(new Runnable()
            public void run()

    } catch (IOException ex)
        Logger.getLogger(DataPage.class.getName()).log(Level.SEVERE, null, ex);

The main window FXML:

<fx:root type="javafx.scene.layout.StackPane" xmlns:fx="" fx:controller="com.clientgui.ClientGUI" prefHeight="675" prefWidth="1200.0" fx:id="root" styleClass="root">

The FXML of the box I want to create dynamically:

<VBox id="VBox" prefHeight="400.0" prefWidth="600.0" xmlns:fx="" fx:controller="com.clientgui.StaticDataBox" styleClass="data-box">
11/6/2013 4:12:19 PM

You are using the instantiated version of FXMLLoader, i.e. non-static load() method of it. This method obligates that the location is set prior calling it, as explained in its javadoc. So by calling loader.load() method, FXMLLoader parses the fxml file at a given location, initializes the controller and constructs the node graph. If the loader.load() method invoked again, FXMLLoader detects that the root has been already set and throws the "Root value already specified." exception.
However invoking the static load() methods of FXMLLoader over and over, will not cause this exception. Because the fxml file parsing and other stuff are performed from the scratch over again independently from each call, on each call.

11/7/2013 8:06:35 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow