FXMLLoader in JavaFx - How To

FXMLLoader

With the FXMLLoader you can load *.fxml files generated with SceneBuilder into your Application.

For example:

Parent parent;
        try {
            parent = FXMLLoader.load(getClass().getResource("/fxml/Settings.fxml"));
        } catch (IOException e) {
            Log.e(TAG, "could not load fxml");
            e.printStackTrace();
            return;
        }

Keep in mind that you have to define a resource directory in your IDE. In in IntelliJ IDEA just right click at your resource folder and 'Mark Directory as' - there you go.

The Log Class, is a Logger class inspired from Android. At the moment it just wraps the System.out.println() command. At the start of my appilcation I initalize my Logger with the logging mode like: user, error, info, debug, verbose. If I initalize him with verbose it prints all my Logs.

I know there is a Java-build in Logger - but I don't need this big functionality. And in development I just need a simple logger, and later I don't need to remove all my System.out.println() in my code.

Later in your Code you have to do something with this Parent for example, add it to an VBox, HBox, Pane etc. I use an construct where I switch Pages, which are essential VBoxes to my Anchor Pane, I keep all my Pages in an List and switch to it when the user presses a button.

To add the loaded Parent as a Child into your Vbox oder AnchorPane etc. just do:

anchorPane.getChildren().addAll(parent);

To set the Parent to

So for every Page I have a Java-Class e.g. 'SettingsPage' which extends VBox. In this Constructor I can do my FXML-Loading work, or if I want an more non-static Page I add Items in code.

public class SettingsPage extends VBox { ... }