JavaFx嵌套控制器(FXML< include>) [英] JavaFx Nested Controllers (FXML <include>)
问题描述
在此计划中,是一个示例如何包含自定义组件并从容器的控制器使用其控制器。
In this tutotial, is an example of how to include custom components and use their controllers from the controller of the container.
main_window_content.fxml
main_window_content.fxml
<VBox fx:controller="com.foo.MainController">
<fx:include fx:id="dialog" source="dialog.fxml"/>
...
</VBox>
MainController.java
MainController.java
public class MainController extends Controller {
@FXML private Window dialog;
@FXML private DialogController dialogController;
..
如果组件只包含一次, 。
如果两次包含相同的组件,则不会初始化控制器。
两个控制器都为null。
If the component is included only once, it works fine. If the same component is included twice, the controllers are not initialized. Both controllers are null.
main_window_content.fxml
main_window_content.fxml
<VBox fx:controller="com.foo.MainController">
<fx:include fx:id="dialog1" source="dialog.fxml"/>
<fx:include fx:id="dialog2" source="dialog.fxml"/>
...
</VBox>
MainController.java
MainController.java
public class MainController extends Controller {
@FXML private Window dialog1;
@FXML private DialogController dialogController1;
@FXML private Window dialog2;
@FXML private DialogController dialogController2;
有人可以帮我解决问题吗?
感谢
Could someone help me to solve the problem? thanks
这是我的FXML加载代码。它在主应用程序方法中执行:
This is my FXML loading code. It is executed in main application method:
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("main_window_content.fxml"));
stage.setTitle("FXML Welcome");
stage.setScene(new Scene(root, 300, 275));
stage.show();
}
推荐答案
感谢Daniel )我发现我的代码中的错误,我的控制器变量的名称是错误的。他们应该< fx:id> Controller
。
换句话说,它应该是:
Thanks to Daniel (from OTN) I found the error in my code, the names of my controller variables were wrong. They should be <fx:id>Controller
.
In other words it should be:
MainController.java
public class MainController extends Controller {
@FXML private Window dialog1;
@FXML private DialogController dialog1Controller;
@FXML private Window dialog2;
@FXML private DialogController dialog2Controller;
但是研究版本2.2中引入的更改我发现一切都可以很容易地通过使用< fx:root>
b(喜欢本教程)。
我在FXML中输入了我的组件,只是这样声明:
But studying the changes introduced in version 2.2 I found that everything can be easily solved by using <fx:root>
tag
(like this tutorial).
I entered my component in FXML simply declaring it like this:
<HBox>
<Dialog id="dialog1" text="Hello World!"/>
<Dialog id="dialog2" text="Hello World!"/>
</HBox>
我希望有用
这篇关于JavaFx嵌套控制器(FXML< include>)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!