sider sider - 4 months ago 58
Java Question

Nested controller issue in Java FX

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?

Answer

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.

See Nested Ccontrollers on stackoverflow Doc

Comments