MVC Pattern in JavaFX With Scene Builder

2020-05-16 05:19发布

I'm new to JavaFX and am struggling to create a proper MVC architecture given my current setup. I clicked together a UI using Scene Builder and designated a Controller class.

Startup:

public class Portal extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("PortalUI.fxml"));

        stage.setTitle("Portal");
        stage.setScene(new Scene(root));
        stage.show();
    }
}

And the Controller class contains the rest of the code.

public class AccommodationPortalView implements Initializable {
    @Override
    public void initialize(URL url, ResourceBundle resources) {
    // Work here.
    }
}

My professor asked that I further separate the concerns and responsibilities of this application. The Controller is not only managing state and talking with the backend, but also updating the View.

My first response was to let the Controller class become the View and create two other classes for the Controller and Model.

However, I'm at a loss at how to connect these pieces. I never need to instantiate the View, so there is no View instance that I can pass to my Controller, for example. Next, I tried making them all singletons and simply letting Controller fetch them at runtime, but that gives me an error.

public class Portal extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("PortalUI.fxml"));

        stage.setTitle("Portal");
        stage.setScene(new Scene(root));
        stage.show();

        // Controller gets a View and Model instance in initialize();
        // Error: Instantiation and Runtime Exception...
        PortalController.INSTANCE.initialize();
    }
}

How do I properly set-up an MVC pattern using my current configuration? Is a different architecture required?

1条回答
倾城 Initia
2楼-- · 2020-05-16 06:14

Your,
-- View is a primary Stage provided by the JavaFX platform at start up. This stage has the only Scene (you have created and set) which in turn has a parent node content root (your variable). This root node is set by FXMLLoader and represents the layout/node structure defined in the "PortalUI.fxml" file.
In other words Stage -> Scene -> PortalUI.fxml(root) will define the view part.

-- Controller is the class that implements Initializable and that you specified in your PortalUI.fxml file with fx:controller=" " attribute. The class you have specified there (PortalController I suppose) will be created and invoked its initialize() method by the FXMLLoader. Namely the Controller will be created when the PortalUI.fxml file is loaded, so you don't need to create and initialize it yourself. To get the created/initialized instance of the controller from the FXMLLoader look the Accessing FXML controller class.

-- Model is the underlying data structure stored and managed by the controller. It can be anything representing the "data". For example, Person, PortalInfo etc. classes.

查看更多
登录 后发表回答