Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting a class Controller for Anchor Pane

When working with JavaFX Scene Builder encountered the following problem...

Given:

A file fxml, containing description Anchor Pane (fxml formed from Scene Builder);
For Anchor Pane is not specified Controller Class.
This fxml loaded into the Java Application by using FXMLLoader.

Need:

After downloading the Anchor Pane set the value to Controller Class.
It is necessary for to load the same fxml with different handlers.

Question: is it possible, and if so - how to implement?

like image 236
Rams Avatar asked Jan 16 '13 13:01

Rams


1 Answers

The controller class of the loading FXML file can also be set through the Scene Builder. But you want to set it at loading time in the application. To achieve that you should set the controller of the FXMLLoader before the load() method is called:

AnchorPane rootPane;
MyController controller = new MyController();
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("my.fxml"));
fxmlLoader.setRoot(rootPane);
fxmlLoader.setController(controller);
fxmlLoader.load();
like image 114
Uluk Biy Avatar answered Oct 04 '22 14:10

Uluk Biy