M. Ko
M. Ko

Reputation: 563

State persistence between child views and controllers

I have a Java FX application with has a main view (FXML) and its controller. I am trying to simulate a tabpane like child views(FXML) with their own controllers. So far so good, I am able to switch between the child views with the following code :

//page type para is the path of FXML file
private void changePage(String pageType) throws IOException
{
    childScene.getChildren().clear();
    FXMLLoader loader = new FXMLLoader(getClass().getResource(pageType));

    loader.setControllerFactory((Class<?> controllerClass) ->
    {
        //trying to load a single instance controller of the child
        if (controllerClass == EngTaiController.class)
        {
            eng2TaiController.setData(words, selectedFont);
            return eng2TaiController;
        }
        try
        {
            return controllerClass.newInstance();
        }catch (Exception ex)
        {
            throw new RuntimeException(ex);
        }
    });
    Parent page = loader.load();
    VBox.setVgrow(page, Priority.ALWAYS);
    //childScene is the container for childviews
    childScene.getChildren().add(page);
}

The problem is that it doesn't persist any user state, text in textboxes, selection in list view, loaded custom controls in child views when switched from one view to another.

Upvotes: 0

Views: 141

Answers (0)

Related Questions