gpt4 book ai didi

java - FXMLLoader 获取 Controller 返回 null

转载 作者:行者123 更新时间:2023-11-30 09:03:48 24 4
gpt4 key购买 nike

我这里需要一个 FXML 加载的递归案例。

如果我选择查看一个目标,它会将我带到另一个加载策略对象列表的屏幕实例。如果我选择查看策略,它会将我带到另一个加载策略对象列表的屏幕实例。如果我查看一个策略,它会将我带到另一个加载任务对象列表的屏幕实例。

A picture of my UI

当然,我决定使用基 Controller 类 ViewChildItemController 来处理继承。然后我从它扩展了 ViewObjective、ViewStrategy 和 ViewTactic。 (ViewTask 没有意义,因为 Task 是没有子项的最低级别项目)。

问题是,当我使用 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 Hook 到 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com