子控制器没有被注入到主控制器(Subcontroller not being injected in

2019-07-19 16:32发布

我有一个BorderPane(具有相关联的MainController),则FXML为BorderPane用途<fx:include>以包括标签(与控制器StatusBarController)插入BorderPane的底部区域。 不幸的是,该StatusBarController没有注入MainController类的实例,我不明白为什么。

main.fxml:BorderPane使用附带的状态栏

<fx:root type="javafx.scene.layout.BorderPane" fx:id="borderPane" xmlns:fx="http://javafx.com/fxml" fx:controller="com.example.controllers.MainController">
  <bottom>
    <fx:include source="statusbar.fxml" />
  </bottom>
</fx:root>

statusbar.fxml:标签和它相关的控制器

<Label fx:id="statusbar" text="A label simulating a status bar" xmlns:fx="http://javafx.com/fxml" fx:controller="com.example.controllers.StatusBarController" />

同为StatusBarController一个领域MainController:

public class MainController
{
    @FXML
    private StatusBarController statusbarController; // PROBLEM HERE: I would expect the StatusBarController to be injected but this does not happen!


    // Implementing Initializable Interface no longer required on JavaFX 2.2 according to
    // http://docs.oracle.com/javafx/2/fxml_get_started/whats_new2.htm
    // (I also tested this, the initialize() method is being called)
    @SuppressWarnings("unused") // only called by the FXMLLoader
    @FXML // This method is called by the FXMLLoader when initialization is complete
    private void initialize() {
        // initialize your logic here: all @FXML variables will have been injected
        assert borderPane != null : "fx:id=\"borderPane\" was not injected: check your FXML file 'main.fxml'.";
        System.out.println("MainController initialize()");

        //statusbarController.setStatusText("Hello from MainController"); // PROBLEM HERE: this fails because statusbarController is not injected as expected
    }
}

而应用程序的启动:

public void start(Stage primaryStage) 
    {       
        Parent root = null;

        try {
            root = FXMLLoader.load(getClass().getResource("/resources/main.fxml"));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        primaryStage.setTitle("Demo");
        primaryStage.setScene(new Scene(root, 800, 600));
        primaryStage.show();
    }

我的样品的完整的源代码可在http://codestefan.googlecode.com/svn/trunk/SubcontrollerAccess/

所以,问题是:为什么不StatusBarController注入MainController的statusbarController变量?

感谢您的任何提示!

Answer 1:

要使用@FXML标签必须提供fx:id

更新您的main.fxml

<bottom>
    <fx:include fx:id="statusbar" source="statusbar.fxml" />
</bottom>

之后,你可以在你下次使用常量MainController.java

@FXML
Label statusbar; // node itself
@FXML
private StatusBarController statusbarController; // controller

注意, statusbarController不是partialy小写的类名,但fx:id + Controller字。



Answer 2:

似乎是在NetBeans 8.0嵌套fxmls的错误也是如此。 可以在NetBeans的不算创建嵌套FXML的控制器对象适合你,它需要手动插入您的MainController。 每个控制器在NetBeans更新时它就会被消灭了,因此它可以是一种单调乏味。 在这个例子中,将被插入

@FXML私人StatusBarController statusbarController;

行手动成在这种情况下,主控制器,然后它正常工作。 组织大型fxmls /控制器是非常有用的。



文章来源: Subcontroller not being injected into main controller