Nested controller issue in Java FX

左心房为你撑大大i 提交于 2019-12-17 14:55:08

问题


I'm trying to include controller(SelectedIssueController) in my main layout (main.fxml). But I get the following error:

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

Line in main.fxml:

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

My selectedissue.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>

Line in MainController:

@FXML
private SelectedIssueController selectedIssueController;

As I understand it injects VBox object now, but I need SelectedIssueController. What is wrong with my current implementation?


回答1:


The FXMLLoader appends Controller to the fx:id specified in the fx:include element to get the name of the field to inject the controller to. Therefore it should be either:

@FXML
private SelectedIssueController selectedIssueControllerController;

or

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

The value injected to the field without the Controller suffix is the value created for the fx:include, i.e. the Object created for the root of the included fxml.



来源:https://stackoverflow.com/questions/36113453/nested-controller-issue-in-java-fx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!