我正在尝试在主布局(main.fxml)中包含controller(SelectedIssueController)。但是我收到以下错误:

Can not set lt.mypackage.controllers.SelectedIssueController field lt.mypackage.controllers.MainController.selectedIssueController to javafx.scene.layout.VBox


main.fxml中的行:

  <fx:include fx:id="selectedIssueController" source="controllers/selectedissue.fxml" />


我选择的issue.fxml:

<VBox xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="lt.mypackage.controllers.SelectedIssueController" fillWidth="false" SplitPane.resizableWithParent="false">
    <children>
     .....
     .....
    </children>
</VBox>


MainController中输入:

@FXML
private SelectedIssueController selectedIssueController;


据我了解,它现在注入VBox对象,但是我需要SelectedIssueController。我当前的实现方式有什么问题?

最佳答案

FXMLLoaderController附加到fx:id元素中指定的fx:include上,以获取将控制器注入到的字段的名称。因此,它应该是:

@FXML
private SelectedIssueController selectedIssueControllerController;


要么

<fx:include fx:id="selectedIssue" source="controllers/selectedissue.fxml" />


注入到不带Controller后缀的字段的值是为fx:include创建的值,即为包含的fxml的根创建的Object

关于java - Java FX中的嵌套 Controller 问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36113453/

10-13 09:08