Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC Pattern in JavaFX With Scene Builder

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?

like image 937
IAE Avatar asked Jun 03 '12 17:06

IAE


People also ask

What is MVC in JavaFX?

JavaFX enables you to design with Model-View-Controller (MVC), through the use of FXML and Java. The "Model" consists of application-specific domain objects, the "View" consists of FXML, and the "Controller" is Java code that defines the GUI's behavior for interacting with the user.

Is FXML MVC?

In JavaFX, the FXML file used to generate the View also provides a critical role in MVC loading. The FXML file should contain a document reference to the Controller object. This facilitates loading of Controller and binding it with the View at initialization.

Can you use JavaFX without FXML?

You don't have to use FXML or SceneBuilder. You can simply create the objects yourself and add them to your Scene/Stage yourself. It's entirely open as to how you implement it.


1 Answers

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.

like image 145
Uluk Biy Avatar answered Sep 20 '22 19:09

Uluk Biy