Javafx FXMLLoader.getController() 方法 returns 空
Javafx FXMLLoader.getController() method returns null
在我的主循环中创建显示时,AnchorPane FXML 的加载程序 returns 在调用 getController() 时为空。
//instantiates the FXMLLoader class by calling default constructor
//creates an FXMLLoader called loader
FXMLLoader loader = new FXMLLoader();
//finds the location of the FXML file to load
loader.setLocation(mainApp.class.getResource("/wang/garage/view/ItemOverview.fxml"));
//sets the AnchorPane in the FXML file to itemOverview
//so that the AnchorPane is set to the display of the app
AnchorPane itemOverview = (AnchorPane) loader.load();
rootLayout.setCenter(itemOverview);
//finds the controller of the itemOverview and
//sets it to controller variable
//then provides a reference of mainApp to controller to connect the two
ItemOverviewController controller = loader.getController();//returns null
controller.setMainApp(this);
我没有在 FXML 文档中指定控制器。如果我使用 loader.load(),是否有必要这样做?如果是这样,我应该如何在FXML文档中指定控制器?
如果您不直接在 Java 代码中设置控制器,则需要在 FXML 文件中指定控制器 class(否则 FXMLLoader
将没有关于它应该创建什么样的对象来用作控制器)。
只需添加
fx:controller="com.mycompany.myproject.ItemOverViewController
以通常的方式为 FXML 文件的根元素添加属性。
或者,您可以从 Java:
设置控制器
//instantiates the FXMLLoader class by calling default constructor
//creates an FXMLLoader called loader
FXMLLoader loader = new FXMLLoader();
//finds the location of the FXML file to load
loader.setLocation(mainApp.class.getResource("/wang/garage/view/ItemOverview.fxml"));
// create a controller and set it in the loader:
ItemOverviewController controller = new ItemOverviewController();
loader.setController(controller);
//sets the AnchorPane in the FXML file to itemOverview
//so that the AnchorPane is set to the display of the app
AnchorPane itemOverview = (AnchorPane) loader.load();
rootLayout.setCenter(itemOverview);
//provide a reference of mainApp to controller to connect the two
controller.setMainApp(this);
在我的主循环中创建显示时,AnchorPane FXML 的加载程序 returns 在调用 getController() 时为空。
//instantiates the FXMLLoader class by calling default constructor
//creates an FXMLLoader called loader
FXMLLoader loader = new FXMLLoader();
//finds the location of the FXML file to load
loader.setLocation(mainApp.class.getResource("/wang/garage/view/ItemOverview.fxml"));
//sets the AnchorPane in the FXML file to itemOverview
//so that the AnchorPane is set to the display of the app
AnchorPane itemOverview = (AnchorPane) loader.load();
rootLayout.setCenter(itemOverview);
//finds the controller of the itemOverview and
//sets it to controller variable
//then provides a reference of mainApp to controller to connect the two
ItemOverviewController controller = loader.getController();//returns null
controller.setMainApp(this);
我没有在 FXML 文档中指定控制器。如果我使用 loader.load(),是否有必要这样做?如果是这样,我应该如何在FXML文档中指定控制器?
如果您不直接在 Java 代码中设置控制器,则需要在 FXML 文件中指定控制器 class(否则 FXMLLoader
将没有关于它应该创建什么样的对象来用作控制器)。
只需添加
fx:controller="com.mycompany.myproject.ItemOverViewController
以通常的方式为 FXML 文件的根元素添加属性。
或者,您可以从 Java:
设置控制器//instantiates the FXMLLoader class by calling default constructor
//creates an FXMLLoader called loader
FXMLLoader loader = new FXMLLoader();
//finds the location of the FXML file to load
loader.setLocation(mainApp.class.getResource("/wang/garage/view/ItemOverview.fxml"));
// create a controller and set it in the loader:
ItemOverviewController controller = new ItemOverviewController();
loader.setController(controller);
//sets the AnchorPane in the FXML file to itemOverview
//so that the AnchorPane is set to the display of the app
AnchorPane itemOverview = (AnchorPane) loader.load();
rootLayout.setCenter(itemOverview);
//provide a reference of mainApp to controller to connect the two
controller.setMainApp(this);