0

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!

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the advice, I'll look into it. In the meantime, in the main class I created a variable "public static VideoRouterFXMLController EVIFcontroller;" and right below it a public static method that returns that value. In the code snippet I shared above I changed the last line so the public variable is the one populated by the getController. In the external class I call VideoRouterFXMLController EVIFcontroller = VideoRouter.getMainController(); EVIFcontroller.populateRemoteSourcesPane(); but the method call throws a null pointer exception. Ideas? Thanks again!
Can you check whether your method call (EVIFcontroller.populateRemoteSourcesPane()) is before or after you set the controller variable? Also why would you have its accessibility as public instead of private since you're exposing it via a getter?
You are right, that should have been private; corrected. I'll have to get back to you on the rest, thanks for the insight.
Bingo. When I called it as a test it was being called too soon. I mapped the external call to a button on the GUI and when I clicked that button I seem to be getting the correct behavior. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.