Multiple Controllers with Javafx 2, as a reference to each other’s instance

Asked

Viewed 886 times

11

UPDATED.

I am creating a new Java application using Javafx, when programming with Swing I had the habit of creating multiple specialized controllers for the interface, and in Swing I use a controller that has knowledge of all others and so with Lazy methods I load each as necessary, and discard when absolutely sure I will not need more, for example when the respective View is discarded.

I have noticed that Javafx takes care of the creation of Controller for me, but I have not figured out how to get a secondary controller to know the main one. And worse there are cases I have the same Controller in several `Viewers.

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

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 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’s been bothering me is that I need to carry the Viewer to get the controller, and if I reverse and start injecting the controller through the code, I lose the functionality of getting automatic references to Handlers defined internally in each controller.

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

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

  • 1

    I found the solution using Jboss WELD with CDI, plus a little code, was excellent, but complex to respond immediately in summary and practical, I will be writing as I did and once I have time put. .

1 answer

2

Maybe this helps 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 define it programmatically, 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;
}

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.