首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >FXMLLoader get控制器返回null

FXMLLoader get控制器返回null
EN

Stack Overflow用户
提问于 2014-08-28 02:25:54
回答 1查看 1.8K关注 0票数 2

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

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

很自然,我决定使用一个基本控制器类ViewChildItemController来处理继承。然后我在此基础上扩展了ViewObjective、ViewStrategy和ViewTactic。(ViewTask没有任何意义,因为任务是没有子项的最低级别的项)。

问题是,当我使用loader.loadController()时,该方法返回null。

代码语言:javascript
复制
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副本,并将控制器分别挂接到每个ViewObjectiveController、ViewStrategyController等?这样做没有多大意义。

我可以尝试loader.setController(),但我不确定是否会再次映射@FXML属性。

EN

回答 1

Stack Overflow用户

发布于 2014-08-28 02:35:43

事实证明,我所要做的就是将控制器视为“动态”。

也就是说,在加载根之前设置控制器。

代码语言:javascript
复制
@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);
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25534480

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档