SceneManager Singleton - laurence-trippen/FXUtils GitHub Wiki

The SceneManager class is a singleton container.

The singleton will be lazy initialized.

SceneManager sceneManager = SceneManager.getSceneManager();

To load a scene from a FXML-Document call the loadScene method. The loadScene method needs two parameters (id, path).

The id is required to access the scene later.

SceneManager.getSceneManager().loadScene("MainScene", "../MainScene.fxml");

Best practise is to lazy initialize the singleton in the Application init() method from JavaFX.


public class App extends Application {
	
	@Override
	public void init() throws Exception {
		super.init();
		SceneManager sceneManager = SceneManager.getSceneManager();
		sceneManager.loadScene("mainScene", "....MainScene.fxml");
	}
	
	@Override
	public void start(Stage primaryStage) throws Exception {
		primaryStage.setScene(SceneManager.getSceneManager().getSceneById("mainScene"));
		primaryStage.setTitle("App");
		primaryStage.show();
	}
	
	public static void main(String[] args) {
		Application.launch(args);
	}
}

To get the scene from the SceneManager you must call only:

Scene scene = SceneManager.getSceneManager().getSceneById("mainScene");

You can also access the controller from scene:

MainSceneController controller = (MainSceneController) SceneManager.getSceneManager().getControllerById("mainScene");