How to get controller form FXML file?

Sometime we need to access controller class in order to access the controls. From changing the text on a label of child scene or to pass some data to the controller, we need controller class.

So to get the controller class of FXML file we are loading we use following code:

FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/FXML.fxml"));
Parent root = loader.load();
// Access Controller as following 
FXMLController fxmlController = loader.getController();
Scene scene = new Scene(root);

Leave a Reply

Your email address will not be published. Required fields are marked *