我这里需要 FXML 加载的递归情况。
如果我选择查看目标,它会将我带到加载策略对象列表的屏幕的另一个实例。如果我选择查看策略,它会将我带到加载战术对象列表的屏幕的另一个实例。如果我查看策略,它会将我带到加载任务对象列表的屏幕的另一个实例。
自然地,我决定使用基本 Controller 类 ViewChildItemController 来处理继承。然后我从中扩展了 ViewObjective、ViewStrategy 和 ViewTactic。 (ViewTask 没有意义,因为任务是最低级别的项目,没有子项)。
问题是,当我使用 loader.loadController() 时,该方法返回 null。
FXMLLoader loader = new FXMLLoader(this.getClass()
.getResource(ScreenPaths.VIEW_PLAN_ITEM));
Parent root = null;
try {
root = loader.load();
} catch (IOException e) {
}
ViewObjectiveController ctrl = loader.getController();//Return null? Why?
ObservableList<Strategy> childItems = childItemsTableView.
getSelectionModel().getSelectedItem().getChildPlanItems();
ctrl.initValues(childItems);
DialogPopup.showNode(root);
是否正在加载的基本 FXML 已挂接到 ViewChildItemController?我是否必须创建 FXML 的多个副本并将 Controller 分别连接到每个 ViewObjectiveController、ViewStrategyController 等?这样做没有多大意义。
我可以尝试 loader.setController(),但我不确定 @FXML 属性是否会再次映射。
最佳答案
事实证明我所要做的就是将 Controller 视为“动态”。
也就是说,在加载根之前设置 Controller 。
@Override
protected void viewChildItem() {
FXMLLoader loader = new FXMLLoader(this.getClass()
.getResource(ScreenPaths.VIEW_PLAN_ITEM));
ViewTacticController ctrl = new ViewTacticController();
loader.setController(ctrl);
Parent root = null;
try {
root = loader.load();
} catch (IOException e) {
}
ObservableList<Task> childItems = childItemsTableView.
getSelectionModel().getSelectedItem().getChildPlanItems();
ctrl.initValues(childItems);
DialogPopup.showNode(root);
}
关于java - FXMLLoader 获取 Controller 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25534480/