javajavafxreloadborderpane

How to prevent reloading a scene inside a borderpane?


I'm writing my first JavaFX application and am using a launcher class (Launcher.java) to launch the main scene (Main.java) which uses MainMenuController.java that has a borderpane. On init I'm loading another Controller in the center of the borderpane where the actual operations are being done.

Launcher:

public class Launcher {
    static final String CONFIGPATH = System.getProperty("user.dir") + "\\config.properties";

    public static void main(String[] args) {
        Application.launch(Main.class, args);
    }

}

Main:

public class Main extends Application {
    Parent root;
    Stage stage;

    @Override
    public void start(Stage primaryStage) {
        try {
            root = FXMLLoader.load(Objects.requireNonNull(getClass().getResource("main-menu.fxml")));
            stage = primaryStage;
            stage.setTitle("Helvetia OpenShift Helper");
            stage.setHeight(850);
            stage.setWidth(1100);

            Scene scene = new Scene(root);
            stage.setScene(scene);
            stage.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}

MainMenuController:

public class MainMenuController {

    @FXML
    BorderPane mainPane;

    public MainMenuController() {
    }

    //Load DiffChecker on GUI init
    @FXML
    private void initialize() throws IOException {

        DiffCheckerController diffCheckerController = new DiffCheckerController();
        mainPane.setCenter(diffCheckerController);
    }

    //Load specific apps on click
    public void onBtnAClick(ActionEvent actionEvent) throws IOException {
            DiffCheckerController diffCheckerController = new DiffCheckerController();
            mainPane.setCenter(diffCheckerController);
    }
}

So basically, since DiffCheckerController is loaded on init into the center of the borderpane, how can I prevent it being reloaded/reset with the onBtnAClick method? There will be other "apps" inside of this application in the future, which is why it's already been implemented like this.

I tried

public void onBtnAClick(ActionEvent actionEvent) throws IOException {
    if (mainPane.getCenter() != DiffCheckerController) {
        DiffCheckerController diffCheckerController = new DiffCheckerController();
        mainPane.setCenter(diffCheckerController);
    } 
            
}

But that didn't work.


Solution

  • I'm not sure I really understand this question, but there is no need to load the view more than once. Just refactor in the obvious way:

    public class MainMenuController {
    
        @FXML
        BorderPane mainPane;
    
        private DiffCheckerController diffCheckerController ;
    
        public MainMenuController() {
        }
    
        //Load DiffChecker on GUI init
        @FXML
        private void initialize() throws IOException {
    
            diffCheckerController = new DiffCheckerController();
            mainPane.setCenter(diffCheckerController);
        }
    
        //Load specific apps on click
        public void onBtnAClick() {
            mainPane.setCenter(diffCheckerController);
        }
    }