JavaFX 8: Reusing of FXML

javafx javafx-8

267 观看

1回复

1758 作者的声誉

Given I have an FXML which represents a form to create a data record. If I use this FXML only for a CreateRecordController I could bind the controller directly in the FXML and use the fx:ids to bind the controls to this controller.

But what would I do if I want to reuse this FXML for an EditRecordController as well? I cannot bind two controllers in the FXML. Is there a way to do this binding in the FXML loader??

作者: Hannes 的来源 发布者: 2017 年 12 月 27 日

回应 1


2

58364 作者的声誉

You can pass the controller instance to FXMLLoader before loading the fxml. This requires the fx:controller attribute to be absent from the fxml file:

Object controller = ... // create an instance of the desired controller class here

FXMLLoader loader = new FXMLLoader(getClass().getResource("/some/path/myfxml.fxml"));
loader.setController(controller);

Node root = loader.load();

Note: Binding is the wrong term here, since you do not assign the controller to any javafx property.

作者: fabian 发布者: 2017 年 12 月 27 日
32x32