Search code examples
javafxfxmlmenubar

JavaFX FXML: Code menu bar once and implement in multiple FXML files


Is it possible in a FXML-file to include another FXML-file that describes for example a menubar?


Solution

  • Yes it is. For example:

    FXMLLoader loader = 
    new FXMLLoader(this.getClass().getResource("YourFXML.fxml"));
                tabPane.getTabs().get(0).setContent((Node) loader.load());
    

    With the setContent Method I could set a FXML as the Content of this tab. In other panes you could do the same. An anchorpane could do:

    anchorpane.getChildren().add((Node) loader.load());
    

    Hope that helps you ;)