I would like to communicate with a FXML controller class at any time, to update information on the screen from the main application or other stages.
Is this possible? I havent found any way to do it.
Static functions could be a way, but they don't have access to the form's controls.
Any ideas?
You can get the controller from the
FXMLLoader
store it in your main stage and provide getFooController() getter method.
From other classes or stages, every time when you need to refresh the loaded "foo.fxml" page, ask it from its controller:
updatePage() can be something like:
in the FooController class.
This way other page users do not bother about page's internal structure like what and where
Label lblData
is.Also look the https://stackoverflow.com/a/10718683/682495. In JavaFX 2.2
FXMLLoader
is improved.Just to help clarify the accepted answer and maybe save a bit of time for others that are new to JavaFX:
For a JavaFX FXML Application, NetBeans will auto-generate your start method in the main class as follows:
Now, all we need to do to have access to the controller class is to change the FXMLLoader
load()
method from the static implementation to an instantiated implementation and then we can use the instance's method to get the controller, like this:On the object's loading from the Main screen, one way to pass data that I have found and works is to use lookup and then set the data inside an invisible label that I can retrieve later from the controller class. Like this:
This works, but there must be a better way.
Another solution is to set the controller from your controller class, like so...
This is the solution I prefer to use since the code is somewhat messy to create a fully functional FXMLLoader instance which properly handles local resources etc
versus