Multiple Controllers with JavaFX 2, how one reference the other's instance

Question:

UPDATED.

I'm creating a new Java application using JavaFX, when I was programming with Swing I used to create multiple specialized controllers for the interface, and in Swing I use a controller that has knowledge of all the others and so with lazy methods I load each one as needed, and I discard it when I'm absolutely sure I won't need it anymore, for example when the respective View is discarded.

I've already noticed that JavaFX takes care of creating the Controller for me in a way, but I haven't figured out how I can make a secondary Controller aware of the main one. And worse there are cases where I have the same Controller in several `Viewers.

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

In the second case, having the same controller for two different Viewers , how can I control them to be a Singleton (the same instance for both)?

The code used today to get the controllers 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 the Viewer to get the controller, and if I invert and start injecting the controller through code, I lose the functionality of getting automatic references to the Handlers defined internally in each controller.

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

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

Answer:

Maybe this will help you, the controller can be created without being directly linked to fxml. That way you can create a constructor and pass as a parameter any object it needs. For that in fxml you remove the attribute that defines the controller class, and define it programmatically, your method to 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;
}

}

Scroll to Top