I am really new in javafx development and I want to ask some questions about the best practices and about my issue.
Well I am working ona yugioh! game and I started to develop the interfaces. I tried to do like with react ( working with components and reuse them instead of making interfaces for every case). So I studied yesterday the nested controllers and tried to do as told.However, the subController returns NULL.
Here is the deck builder interface (main interface)
<AnchorPane fx:controller="controller.DeckBuilderController">
<children>
<BorderPane fx:id="cardBuilderPane" >
<left>
<fx:include fx:id="CardInfos" source="CardInfosInterface.fxml" />
</left>
...
</AnchorPane>
here is the deck builder controller
public class DeckBuilderController implements Initializable{
@FXML
AnchorPane CardInfos;
@FXML
CardInfosController cardInfosController ;
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
System.out.println( CardInfos);
System.out.println(cardInfosController);
}
}
Here is the cardInfos interface (subInterface)
<AnchorPane fx:controller="controller.CardInfosController">
....
</AnchorPane>
Here is the CardInfosController
public class CardInfosController implements Initializable {
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
System.out.println("init");
}
And the result of my execution
init
AnchorPane[id=CardInfos]
null
CodePudding user response:
The fx:id
for the included FXML is not correctly matching the field name for the controller. The rule for "nested controllers" is that the field name for the nested controller should be the fx:id
with "Controller"
appended. You have
fx:id="CardInfos"
but
@FXML
CardInfosController cardInfosController ;
Appending "Controller"
to the fx:id
gives "CardInfosController"
, not "cardInfosController"
.
To fix this in a way that conforms to standard Java naming conventions, you should fix the fx:id
, not the field name. This will also mean you have to fix the field name for the included element:
<AnchorPane fx:controller="controller.DeckBuilderController">
<children>
<BorderPane fx:id="cardBuilderPane" >
<left>
<fx:include fx:id="cardInfos" source="CardInfosInterface.fxml" />
</left>
...
</AnchorPane>
and
public class DeckBuilderController implements Initializable{
@FXML
AnchorPane cardInfos;
@FXML
CardInfosController cardInfosController ;
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
System.out.println(cardInfos);
System.out.println(cardInfosController);
}
}