1

I am developing a software using JavaFX. It is a desktop application. I am new to MVC, and also JavaFX; But learn some details by googling. I follow the steps here, to learn about JavaFX and also MVC. I know that in MVC, Model is a POJO, View is visualization, and controller acts on both, accept input and convert to commands for view and model. The model can also have logic to update the controller. We also for each view, should have a controller.( view-controllers) But I have some question; Why in the tutorial , We create both PersonEditDialog and PersonOverview stage in the mainApp? I mean this :

public void showPersonOverview() {
try {
    // Load person overview.
    FXMLLoader loader = new FXMLLoader();
    loader.setLocation(MainApp.class.getResource("view/PersonOverview.fxml"));
    AnchorPane personOverview = (AnchorPane) loader.load();

    // Set person overview into the center of root layout.
    rootLayout.setCenter(personOverview);

    // Give the controller access to the main app.
    PersonOverviewController controller = loader.getController();
    controller.setMainApp(this);

} catch (IOException e) {
    e.printStackTrace();
}

}

And this :

public boolean showPersonEditDialog(Person person) {
try {
    // Load the fxml file and create a new stage for the popup dialog.
    FXMLLoader loader = new FXMLLoader();
    loader.setLocation(MainApp.class.getResource("view/PersonEditDialog.fxml"));
    AnchorPane page = (AnchorPane) loader.load();

    // Create the dialog Stage.
    Stage dialogStage = new Stage();
    dialogStage.setTitle("Edit Person");
    dialogStage.initModality(Modality.WINDOW_MODAL);
    dialogStage.initOwner(primaryStage);
    Scene scene = new Scene(page);
    dialogStage.setScene(scene);

    // Set the person into the controller.
    PersonEditDialogController controller = loader.getController();
    controller.setDialogStage(dialogStage);
    controller.setPerson(person);

    // Show the dialog and wait until the user closes it
    dialogStage.showAndWait();

    return controller.isOkClicked();
} catch (IOException e) {
    e.printStackTrace();
    return false;
}

} Both are defined and created in the mainApp class? Why we did not create the PersonEditDialog stage and scene in the PersonOverviewController? In controllers we should not use new keyword? They just are connectores between view and model? Im asking about controller rules; for multi stage and .fxml software. My software includes 8 different pages( or more ), Should I create all stages in the mainApp ? Why should not create new stage and add the according .fxml to the scene of that stage in previous step of the application?

  • I think the answer to this question depends on the flow of each stage. What I normally do is create all Stages in the Main that will be showing when the program loads/starts and pass the model to them. After that, if a stage is opened using a button, I create the stage and pass the model at that point. Download @James D GitHub example and study it. https://stackoverflow.com/questions/32342864/applying-mvc-with-javafx – SedJ601 May 15 '19 at 15:13

1 Answers1

0

It's not possible to do what you are saying, you can't create something from some other thing that's not even existing. If you don't instantiate the PersonOverview in Main, you can't make it do anything.
Also, notice in this case that the view of PersonOverview is attached to the RootLayout which is created in Main. So you can consider these as one Main View where each controller is managing one part of the view.
In the case of PersonEditDialog, You are starting a stage through the main view to edit some information. That's why it's created in Main. The stage is attached to the MainStage.
And if you have several stages to create, You don't have to do that in Main. It depends on your needs. You can basically run a stage which uses some controller from another controller by clicking on a button for example. So it depends on : after what event you want to see that stage.
Example : You can add a button in PersonEditDialog controller like More... and you define its setOnAction event to open a new view (stage) where you show the picture, the Twitter link...

Mohamed Benmahdjoub
  • 1,270
  • 4
  • 19
  • 38