Tuesday, April 24, 2018

Using multiples FXML files in a javafx application

In this post we assume that you have the knowledge necessary for building your user interface with the JavaFX SceneBuilder.

Usually, one FXMl file represent one screen in the user interface and practically any decent application has more than one.







So how to switch between mutiples fxml files ?


Remember that FXML files are loaded into JavaFX applications by the JavaFX runtime using the FXML Loader class and the result is always a Java object, usually a node such as a Group or a Pane.

So for this purpose we will use a screen controller that holds all of the instances of the fxml files loaded. And every fxml controller will point the instance of that controller.
The screen controller will give the appropriate root to the scene when needed.


public class Controller {
    Pane                     container;
    Map<String, Pane>        screens;  
    public Controller(Pane container) {
        this.container = container;
        screens = new HashMap<>();
    }      

    public void switchContent(String fxml) {            
       container.getChildren().clear();                
       container.getChildren().add(getContent(fxml)); 
    }

    private Pane getContent(String fxml) {        
        if (!screens.containsKey(fxml)) {
            Parent node = loadContent(fxml);
            screens.put(fxml,(Pane) node);
        }        
        return screens.get(fxml);
    }

    private Parent loadContent(String fxml) {
        String path = String.format("%s.fxml", fxml);
        FXMLLoader loader = new FXMLLoader();
        loader.setBuilderFactory(new JavaFXBuilderFactory());
        loader.setLocation(this.getClass().getResource(path));
        Parent page = null;
        try (InputStream in = this.getClass().getResourceAsStream(path)) {
            page = loader.load(in);
            ((Managed) loader.getController()).setScreensController(this);
        } catch (IOException ex) {
            ex.printStackTrace();
        }                     
        return page;
    }         
}