简体   繁体   English

JavaFX 嵌套控制器 (FXML<include> )

[英]JavaFX Nested Controllers (FXML <include>)

In this tutorial, there 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主控制器.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主控制器.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谢谢

This is my FXML loading code.这是我的 FXML 加载代码。 It is executed in the 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(); 
}

Thanks to Daniel (from OTN) I found the error in my code, the names of my controller variables were wrong.感谢 Daniel(来自 OTN),我在代码中发现了错误,我的控制器变量的名称是错误的。 They should be <fx:id>Controller .它们应该是<fx:id>Controller In other words it should be:换句话说,它应该是:

MainController.java主控制器.java

public class MainController extends Controller {
@FXML private Window dialog1;
@FXML private DialogController dialog1Controller;
@FXML private Window dialog2;
@FXML private DialogController dialog2Controller;

But studying thechanges introduced in version 2.2 I found that everything can be easily solved by using <fx:root> tag ( like this tutorial ).但是研究了 2.2 版中引入的更改,我发现使用<fx:root>标签可以轻松解决所有问题( 就像本教程一样)。 I entered my component in FXML simply declaring it like this:我在 FXML 中输入了我的组件,只是像这样声明它:

<HBox>
    <Dialog id="dialog1" text="Hello World!"/>
    <Dialog id="dialog2" text="Hello World!"/>
</HBox>

I hope to be helpful我希望能有所帮助

There seems to be a bug in netbeans 8.0 with nested fxmls as well. netbeans 8.0 中似乎也存在嵌套 fxml 的错误。 Cannot count on netbeans to create the nested fxml's controller object for you, it has to be manually inserted into your MainController.不能指望 netbeans 为您创建嵌套的 fxml 的控制器对象,它必须手动插入到您的 MainController 中。 Every time the controller is updated in netbeans it gets wiped out so it can be kind of tedious.每次在 netbeans 中更新控制器时,它都会被清除,因此可能有点乏味。 For this example that would be inserting the对于此示例,将插入

@FXML private DialogController dialog1Controller;

line manually into the main controller in this case, then it works normally.在这种情况下手动连接到主控制器,然后它可以正常工作。 Very useful for organizing large fxmls/controllers.对于组织大型 fxmls/控制器非常有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM