FXMLLoader get控制器返回null [英] FXMLLoader get controller returns null

查看:212
本文介绍了FXMLLoader get控制器返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要FXML加载的递归情况。

I have a recursive case of FXML loading needed here.

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

If I choose to View an Objective, it takes me to another instance of the screen that loads a list of Strategy objects. If I choose to view a Strategy, it takes me to another instance of the screen that loads a list of Tactic objects. If I view a Tactic, it takes me to another instance of the screen that loads a list of Task objects.

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

Naturally, I decided to use a base controller class, ViewChildItemController to handle inheritance. Then I extended from it ViewObjective, ViewStrategy, and ViewTactic. (ViewTask makes no sense because a Task is the very lowest level item with no children).

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

The problem is, when I use loader.loadController(), the method returns 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的多个副本并将控制器分别挂钩到每个ViewObjectiveController,ViewStrategyController等?这样做没有多大意义。

Could it be that the base FXML being loaded is hooked to the ViewChildItemController? Do I have to create several copies of the FXML and hook the controllers separately to each ViewObjectiveController, ViewStrategyController, etc? It doesn't make much sense to do.

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

I could try loader.setController(), but I'm not sure if the @FXML attributes will be mapped again.

推荐答案

原来我所要做的就是将控制器视为动态。

Turns out all I had to do was treat the controller as "dynamic".

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

That is, set the controller BEFORE loading the root.

@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);
}

这篇关于FXMLLoader get控制器返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆