0

I create several component with SceneBuilder, my goal is now to use all this files for creating a complete window. But I can't load several FXML, here's what I try to do :

    private MenuBar menuBar;
    private Pane filtersView;

    FXMLLoader loader = new FXMLLoader();

    loader.setLocation(MainWindow.class.getResource("../component/menuBar/MenuBar.fxml"));
    menuBar = (MenuBar) loader.load();

    loader.setLocation(MainWindow.class.getResource("../component/filtersView/FiltersView.fxml"));
    filtersView = (Pane) loader.load();

Here's the returning error :

Root value already specified.

Should I have to create a loader for each component ?

Evans Belloeil
  • 2,413
  • 7
  • 43
  • 76

1 Answers1

2

An FXMLLoader instance has a number of interdependent properties (such as the root and controller) which are set either by parsing the FXML file, or programmatically. Each of these also interacts in various ways with the namespace map held by the FXMLLoader instance.

Because the life-cycle of the FXMLLoader would be highly complex if it could be reused, it is an error to set either the root or the controller more than once. (What should happen to the controller if the root were set to a new value? What about the properties in the namespace?).

Consequently, you should only use an FXMLLoader instance once. Create a new loader for each FXML file you want to load:

FXMLLoader menuLoader = new FXMLLoader();

menuLoader.setLocation(MainWindow.class.getResource("../component/menuBar/MenuBar.fxml"));
menuBar = (MenuBar) menuLoader.load();

FXMLLoader filtersLoader = new FXMLLoader();
filtersLoader.setLocation(MainWindow.class.getResource("../component/filtersView/FiltersView.fxml"));
filtersView = (Pane) filtersLoader.load();
James_D
  • 201,275
  • 16
  • 291
  • 322