Mark Dyson
Mark Dyson

Reputation: 305

Java FXML Application: unsuccessful calling method in controller class from external class

Working on a Java FXML application called EVIF VideoRouter. Main class is then VideoRouter.java and the controller class is called VideoRouterFXMLController.java.

I have a method in the VideoRouterFXMLController.java class that I need to be able to call from other external classes within the application. Based on extensive searching and reading, I altered the FXML loader code in the main VideoRouter.java class to be thus:

    FXMLLoader EVIFloader = new FXMLLoader(this.getClass().getResource("VideoRouterFXML.fxml"));
    Parent EVIFroot = (Parent) EVIFloader.load();
    VideoRouterFXMLController EVIFcontroller = EVIFloader.getController();

Now, as I understand this process, I'm somehow supposed to create a getter in the main VideoRouter.java class that returns the EVIFcontroller instance I've created above? And then I can use that getter in other classes and call the method inside the returned instance?

If so, I'm having difficulty figuring out where exactly to place the getter. Does it go in the controller class, or the main class? In either case it doesn't seem to be able to see the controller instance in order to return it.

Again, I've searched extensively and the related posts I've found to not appear to address this specific problem I'm having. Any help?

Many thanks in advance!

Upvotes: 0

Views: 41

Answers (1)

Gnas
Gnas

Reputation: 718

You have already partly answered your problem: create VideoRouterFXMLController member variable in your VideoRouter class and a getter to expose it, then set it to the controller instance after you load the FXML like in the code snippet your provided.

Having said that, I would propose a different approach to your problem because this approach is generally not a good design due to high coupling between your classes. I would recommend Observer pattern as an alternative approach and EventBus as a library to do this.

Upvotes: 1

Related Questions