title: JavaFX, Open a Screen in same Window
body: <p>I'm trying to implement an application, which has a simple navigation. One Main Menu, 3 Submenus, with another 3 Submenus each.</p>
  
  <p>I need the application open every submenu recursively in the same Window with the Mainmenu as the root screen. I must be able to return to that menu by going via the "Back" Button on each Submenu.</p>
  
  <p>I implemented a Main class, a Controller Class and a FXML-file for EACH (!) Menu and Submenu.</p>
  
  <p>E.g. my Main Menu</p>
  
  <pre><code>      package application;
  
  import org.apache.log4j.Logger;
  import javafx.application.Application;
  import javafx.fxml.FXMLLoader;
  import javafx.stage.Stage;
  import javafx.scene.Scene;
  import javafx.scene.layout.AnchorPane;
  
  public class Main extends Application {
  
      // Initialize Logger
      private static final Logger logger = Logger.getLogger(Main.class);
  
      @Override
      public void start(Stage primaryStage)
      {
          try
          {
              AnchorPane root = (AnchorPane)FXMLLoader.load(getClass().getResource("MainFrame.fxml"));
              Scene scene = new Scene(root,1000,500);
  
              primaryStage.setScene(scene);
              primaryStage.show();
          }
          catch(Exception e)
          {
              e.printStackTrace();
          }
      }
  
      public static void main(String[] args)
      {
          logger.info("Starting application.");
          launch(args);
      }
  }
  </code></pre>
  
  <p>My MainController</p>
  
  <pre><code>    package application;
  
  import org.apache.log4j.Logger;
  
  
  import javafx.application.Platform;
  import javafx.fxml.FXML;
  import javafx.fxml.FXMLLoader;
  import javafx.scene.Parent;
  import javafx.scene.Scene;
  import javafx.scene.control.Button;
  import javafx.scene.layout.AnchorPane;
  import javafx.scene.layout.BorderPane;
  import javafx.stage.Stage;
  import javafx.stage.StageStyle;
  
  public class MainFrameController
  {
  
      private static final Logger logger = Logger.getLogger(MainFrameController.class);
  
      @FXML
      private Button btn_random1;
      @FXML
      private Button btn_random2;
      @FXML
      private Button btn_random3;
      @FXML
      private Button btn_random4;
  
      public void initialize()
      {
          //mainService = new MainService();
      }
  
      @FXML
      private void onRandomButton1() throws Exception
      {
          logger.info("onRandomButton1Clicked");
  
          Stage stage = new Stage();
          AnchorPane root;
          root = (AnchorPane)FXMLLoader.load(getClass().getResource("RandomFXML1.fxml"));
          Scene scene = new Scene(root,1000,500);
          stage.setScene(scene);
          stage.show();
      }
  
      @FXML
      private void onRandomButton2()
      {
          logger.info("onRandomButton1");
      }
  
      @FXML
      private void onRandomButton3()
      {
          logger.info("onRandomButton2");
  
      }
      @FXML
      private void onRandomButton4()
      {
          Platform.exit();
          logger.info("onRandomButton3");
      }
  }
  </code></pre>
  
  <p>Is there a way to simply change my code, so it does open in the same window?</p>
  
  <p>I took a look at several tutorials with relatively complex ways of solving this, I'd like to stick to my code and not changing too much, otherwise I'd have to start all over again.</p>
  
  <p>Pls note, that this is only one of many Main/Controller/FXML combinations, I have about 10 screens and "subscreens", which are being navigated like this (by java opening a new window).</p>
  
  <p>Ideas anyone? Or maybe a relatively simple tutorial (for which I dont have to change my whole code)?</p>
  
  <p>Thanks!</p>