JavaFx 嵌套控制器 (FXML <include>) [英] JavaFx Nested Controllers (FXML <include>)

查看:50
本文介绍了JavaFx 嵌套控制器 (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;

    ..

如果该组件只包含一次,它工作正常.如果两次包含相同的组件,则不会初始化控制器.两个控制器都为空.

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(来自 OTN),我在我的代码中发现了错误,我的控制器变量的名称是错误的.它们应该是 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 版中引入的更改我发现一切可以使用 标签轻松解决(喜欢本教程).我在 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 &lt;include&gt;)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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