在Guice的模块配置中关联FXML和Controller [英] Associating FXML and Controller in Guice's Module configuration

查看:172
本文介绍了在Guice的模块配置中关联FXML和Controller的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Guice模块中,我想关联FXML文件及其控制器,目前看起来像这样:

In my Guice Module I want to associate FXML files and their controllers, currently it looks like this:

public class GuiceModule extends AbstractModule 
{   
    @Override
    protected void configure()
    {
        // associate controllers and fxml files
        bind(MainController.class).toInstance((MainController)loadController("/main.fxml"));
        bind(SubController.class).toInstance((SubController)loadController("/content.fxml"));
    }

    protected Object loadController(String url)
    {
        InputStream fxmlStream = null;
        try
        {
            fxmlStream = getClass().getResourceAsStream(url);
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(getClass().getResource(url));


            loader.setControllerFactory(new Callback<Class<?>, Object>() {

                public Object call(Class<?> clazz) { // clazz because class is a reserved word
                    return injector.getInstance(clazz); // PROBLEM: no access to the injector here
                }
            });

            loader.load(fxmlStream);
            return loader.getController();
        }

        // [..] exception handling

    }
}**strong text**

但是在 loadController(String url)方法中,我遇到了该行的问题:

However in the loadController(String url) method I get problems with that line:

return injector.getInstance(clazz); // PROBLEM: no access to the injector here

如何访问Guice的 Guice模块中的getInstance 方法?这是可能的吗?

How can I access Guice's getInstance method from within a Guice Module? Is that or something equivalent possible?

推荐答案

我是 fx-guice 的作者,一个开放的-source库,可用于在JavaFX应用程序中使用Guice。该库使用Apache License v2许可,可通过中心 Maven存储库

I am the author of fx-guice, an open-source library that can be used to use Guice in your JavaFX applications. The library is licensed using the Apache License v2 and can be obtained via the central Maven repository.

即使它可能回答你的确切问题,我建议你看看我的项目,其中包含了很多例子:

项目主页:→  http://github.com/cathive/fx-guice/

Even though it might not answer your exact question, I suggest you have a look at my project, which comes bundled with quite a few examples:
Project home: → http://github.com/cathive/fx-guice/

我的框架的主要思想是:
而不是扩展javafx.application.Application,而是扩展com.cathive.fx.GuiceApplication。然后,您可以在任何地方@Inject实现GuiceFXMLLoader,并可以使用这些特殊的FXMLLoader实例来加载UI定义。在您的FXML控制器类中,您可以根据需要混合@Inject和@FXML annoations。

→  http://bit.ly/139fKQV

The main idea of my framework is: Instead of extending "javafx.application.Application" you extend "com.cathive.fx.GuiceApplication". You can then simply @Inject instaces of "GuiceFXMLLoader" wherever you want and can use these special FXMLLoader instances to load your UI definitions. Within your FXML-controller classes you can mix @Inject and @FXML annoations as you like.
→ http://bit.ly/139fKQV

我的框架还提供了一系列关于GuicifiedJavaFX组件的功能,它们将Java绑定在一起class和单个FXML文件(使用特殊注释:@ FXMLComponent)。我写了一个简短的计算器示例,其源可以从Github页面获得(见上文)。代码的相关部分可以在这里找到:
$
CalculatorAppPane.java:→  http: //bit.ly/10YMVoM

CalculatorAppPane.fxml:→  http: //bit.ly/13loYv8

My framework also offers a bunch of functionality concerning "Guicified" JavaFX components, which bind together a Java class and a single FXML file (using a special annotation: "@FXMLComponent"). I wrote a short "Calculator" example whose sources can be obtained from the Github pages (see above). The relevant portions of the code can be found here:
CalculatorAppPane.java: → http://bit.ly/10YMVoM
CalculatorAppPane.fxml: → http://bit.ly/13loYv8

希望有所帮助。 : - )

Hope that helps. :-)

这篇关于在Guice的模块配置中关联FXML和Controller的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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