问题描述
我在Scala中使用JavaFX 2.
我有类应用程序扩展javafx.application.Application
它会执行读取应用程序配置等操作。然后它会启动主窗口。
此主窗口需要连接到名为的控制器主窗口
。我可以用 fx:controller =com.foo.bar.MainWindow
来做,但是,在这种情况下, MainWindow 无法访问我的应用程序的配置等。
我想到的一件事是实例化 MainWindow
我自己在 Application
类中调用所有依赖项,然后告诉主窗口视图控制器应该是我刚刚创建的实例。但是,我不知道如何做到这一点?
在JavaFX中访问控制器中数据的首选方法是什么 - 通过某种依赖注入或其他方式? / p>
换句话说,此视图FXML:
<?xml version =1.0encoding =UTF-8?>
<?import javafx.scene.Scene?>
<?import javafx.scene.control。*?>
< BorderPane fx:controller =com.foo.bar.MainWindowprefHeight =703.0prefWidth =803.0xmlns:fx =http://javafx.com/fxml> ;
...
将自动实例化 com.foo。 bar.MainWindow
我的控制器(?):
...
class MainWindow {
var app:Application = null
...
}
其中我有我的应用程序的引用,它包含我想要访问的信息:
class应用程序扩展javafx.application。应用程序{
...
//我要访问的东西,应用程序也会激活第一个场景。
}
问题是如何获得对主应用程序的引用?如何进行依赖注入或任何等效注册?
解决方案 Guice是我注入控制器的首选方式,所有你需要使用的跟随控制器工厂:
class GuiceControllerFactory实现Callback< Class<?>,Object> {
私人最终注射器注射器;
public GuiceControllerFactory(Injector anInjector){
injector = anInjector;
}
@Override
public Object call(Class<?> aClass){
return injector.getInstance(aClass);
}
}
在FXMLLoader上设置:
FXMLLoader loader = new FXMLLoader();
loader.setControllerFactory(controllerFactory);
现在Guice将创建你的控制器。我建议您注入控制器所需的任何服务,而不是传入对应用程序类的引用。
I am using JavaFX 2 with Scala.
I have the class Application extends javafx.application.Application
which does things like reads app configuration, etc. Then it fires up the main window.
This main window needs to be connected to a controller called MainWindow
. I can do it with fx:controller="com.foo.bar.MainWindow"
, however, in this case the instance of MainWindow
is unable to access my application's configuration, etc.
One thing that came to my mind is to instantiate the MainWindow
controller on my own inside the Application
class and inject all dependencies, and then tell the main window view that the controller should be the instance I just created. However, I have no idea how to do this?
What's the preferred way to access data in controllers in JavaFX -- via some sort of dependency injection or something else?
In other words, this view FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.Scene?>
<?import javafx.scene.control.*?>
<BorderPane fx:controller="com.foo.bar.MainWindow" prefHeight="703.0" prefWidth="803.0" xmlns:fx="http://javafx.com/fxml">
...
Will automatically instantiate a com.foo.bar.MainWindow
controller for me(?):
...
class MainWindow {
var app: Application = null
...
}
where I have the reference to my application, which holds information I want to access:
class Application extends javafx.application.Application {
...
// Stuff I want to access, application also fires up the first scene.
}
The problem is that how can I get a reference to the main application? How can I do dependency injection or anything equivalent?
解决方案 Guice is my preferred way of injecting controllers, all you need to use the following controller factory:
class GuiceControllerFactory implements Callback<Class<?>, Object> {
private final Injector injector;
public GuiceControllerFactory(Injector anInjector) {
injector = anInjector;
}
@Override
public Object call(Class<?> aClass) {
return injector.getInstance(aClass);
}
}
Set it on your FXMLLoader:
FXMLLoader loader = new FXMLLoader();
loader.setControllerFactory(controllerFactory);
Now Guice will create your controller. I recommend that you inject in any services your controller needs instead of passing in a reference to your application class.
这篇关于JavaFX控制器如何访问其他服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!