title: Managing scene switching in JavaFX via FXML (Performance Question)
body: <p>I'm playing arround with JavaFX and various scenes that are loaded by FXML. So I got the idea to write a manager that handles scene switching.</p>
  
  <p>So far everything works, but I'm unsure if this is a good implementation.</p>
  
  <pre class="lang-java prettyprint-override"><code>import java.io.IOException;
  import javafx.fxml.FXMLLoader;
  import javafx.scene.Scene;
  import javafx.scene.layout.Pane;
  import javafx.stage.Stage;
  
  public class SceneManager {
      private static final String[] fxmlFiles = {"../gui/MainWindow.fxml", "../gui/NewGameWindow.fxml"};
      private static SceneManager instance = null;
      private static Stage rootStage = null;
  
      private FXMLLoader[] loadedFxml;
      private Pane[] loadedPanes;
      private Scene[] scenes;
  
      public enum States {
          MAIN_MENU, NEW_GAME;
      }
  
      private SceneManager() {
          try {
              this.loadedFxml = new FXMLLoader[States.values().length];
              this.loadedPanes = new Pane[States.values().length];
              this.scenes = new Scene[States.values().length];
  
              for(int i = 0; i &lt; fxmlFiles.length; i++) {
                  loadedFxml[i] = new FXMLLoader(getClass().getResource(fxmlFiles[i]));
                  loadedPanes[i] = loadedFxml[i].load();
                  scenes[i] = new Scene(loadedPanes[i]);
              }
  
              rootStage.setScene(scenes[0]);
              rootStage.setResizable(false);
              rootStage.show();
          } catch(IOException e) {
              e.printStackTrace();
          }
      }
  
      public static SceneManager getInstance() {
          if(instance == null) {
              instance = new SceneManager();
          }
          return instance;
      }
  
      public static void setUp(Stage stage) {
          SceneManager.rootStage = stage;
      }
  
      public void switchScene(States state) {
          rootStage.setScene(scenes[state.ordinal()]);
      }
  }
  </code></pre>
  
  <p>So what I plan to do is, load the FXML via the loader, assign it to a pane, create all scenes.</p>
  
  <p>Then I set a scene as its starting scene and do the rest via the getInstance().switchScene() method in the controller. </p>
  
  <p>It works well but I'm unsure if this is a good approach.</p>