JavaFX custom controller factory

帅比萌擦擦* 提交于 2019-12-04 18:53:00

问题


I have been experimenting with the FXMLLoader and using the setControllerFactory method using a custom Callback<P,R> implementation.

The ORACLE documentation says the following:

An implementation might return a null value to indicate that it does not or cannot create a controller of the given type; in this case, the default controller construction mechanism will be employed by the loader.

The result I want to achieve is that I can use a dependency injection framework to create any controllers that require parameters but I will let the FXMLLoader load any controllers that do not require parameters.

So if I have the following simple FXML file which uses the ViewController class which accepts no parameters...

<StackPane fx:id="pane"
          xmlns:fx="http://javafx.com/fxml"
          fx:controller="my.package.ViewController">
</StackPane>

and I use the following simple controller factory implementation to signal to the FXMLLoader that I want it to manage the construction of the controller in this case...

loader.setControllerFactory(new Callback<Class<?>, Object>(){
    @Override
    public Object Call(Class<?> type) {
        return null; // Let the FXMLLoader handle construction...
    }
});

after calling the load() method my Initialise method in the ViewController class is never called (I have verified this with a breakpoint).

If I change my controller factory implementation to return an instance of the ViewController class then everything works as expected.

Can anyone help me to clear up my confusion? Am I using the Callback interface incorrectly or is the ORACLE documentation incorrect?


回答1:


javafx does the following in FXMLLoader:

    try {
      if (controllerFactory == null) {
        setController(ReflectUtil.newInstance(type));
      } else {
        setController(controllerFactory.call(type));
      }
    } catch (InstantiationException exception) {
      throw new LoadException(exception);
    } catch (IllegalAccessException exception) {
      throw new LoadException(exception);
    }

so, yes, the oracle tutorial is incorrect.



来源:https://stackoverflow.com/questions/19154711/javafx-custom-controller-factory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!