Multiple Controllers with JavaFX 2, such as referencing the instance of the other

11

UPDATED.

I am creating a new Java application using JavaFX, when programming with Swing I had the custom of creating multiple specialized controllers for the interface, and in Swing I use a controller that is aware of all others and so with lazy methods I load each one as needed, and discard when absolute certainty that I will not need more, for example when when the respective View is discarded.

I've come to realize that JavaFX takes some care of creating Controller for me, but I have not figured out how to make a secondary Controller aware of the principal. And worse there are cases that I have the same Controller in several 'Viewers.

For example when opening a new panel through a menu or when clicking a button, how do I make the controller of this new panel access the current instance of the previous panel controller that was responsible for opening this new one.

In the second case, having the same controller for two Viewers different, how to control that they are a Singleton (the same instance for both)?

The code used today to get the drivers is as follows:

SocialStreamController getSocialStreamController() {
    if (socialStreamController == null) {
        FXMLLoader loader = new FXMLLoader();
        URL resource = getClass().getResource("../view/SocialStreamView.fxml");
        loader.setLocation(resource);

        try {
            socialStreamView = loader.load();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }

        socialStreamController = loader.getController();
        socialStreamController.setMainControler(this);

    }
    return socialStreamController;
}

What has bothered me is that I need to load Viewer to get the controller, and if I invert and then inject the controller with the code, I lose the functionality of getting automatic references to Handlers internally defined on each controller .

I always try to maintain a hierarchy where the most specialized and internal controller in the application knows about the previous one, in which it was responsible for its activation.

In addition, are there cases where certain controllers need to access functions and handlers from other controllers how can I include such references to other controllers?

    
asked by anonymous 03.08.2015 / 02:03

1 answer

2

Maybe this will help you, the controller can be created without being directly connected to fxml. This way you can create a constructor and pass as parameter any object it needs. For this in fxml you remove the attribute that defines the controller class, and programmatically sets it, your method would change to something like this:

SocialStreamController getSocialStreamController() {
    if (socialStreamController == null) {
        FXMLLoader loader = new FXMLLoader();
        URL resource = getClass().getResource("../view/SocialStreamView.fxml");
        loader.setLocation(resource);
        socialStreamController = new SocialStreamController(<parametros aqui!>);
        loader.setController(socialStreamController);
        try {
            socialStreamView = loader.load();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }
    }
    return socialStreamController;
}

}

    
03.11.2015 / 13:46