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?
The controller is binded in you fxml file or where you call the main view for the first time. So you can use the fx:controller attribute in the xml or the FXMLLoader#setController() method from your launcher.
This is a JavaFX FXML Controller Example. FXML is an XML-based language designed to build the user interface for JavaFX applications. You can use FXML to build an entire Scene or part of a Scene . FXML allows application developers to separate the logic for building the UI from the business logic.
You can get the controller from the FXMLLoader
FXMLLoader fxmlLoader = new FXMLLoader(); Pane p = fxmlLoader.load(getClass().getResource("foo.fxml").openStream()); FooController fooController = (FooController) fxmlLoader.getController();
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:
getFooController().updatePage(strData);
updatePage() can be something like:
// ... @FXML private Label lblData; // ... public void updatePage(String data){ lblData.setText(data); } // ...
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:
@Override public void start(Stage stage) throws Exception { Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml")); Scene scene = new Scene(root); stage.setScene(scene); stage.show(); }
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:
//Static global variable for the controller (where MyController is the name of your controller class static MyController myControllerHandle; @Override public void start(Stage stage) throws Exception { //Set up instance instead of using static load() method FXMLLoader loader = new FXMLLoader(getClass().getResource("FXMLDocument.fxml")); Parent root = loader.load(); //Now we have access to getController() through the instance... don't forget the type cast myControllerHandle = (MyController)loader.getController(); Scene scene = new Scene(root); stage.setScene(scene); stage.show(); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With