Cześć, pozwolę sobie wkleić po angielsku bo podobne pytanie zadałem na stacku, ale póki co żadnej odpowiedzi. Może tutaj ktoś mi pomoże. Wygląda na to, że jak jeden fxml załącza drugi to DI nie działa. Nie rozumiem dlaczego.
I introduced Google Guice to my JavaFx applicatiomn. However I have some problem when I try to @Inject my controller into another controller.
First of all I do:
loader.setControllerFactory(Main.getInjector()::getInstance);
MainController has TopMenuButtonsController included in fxml file:
<fx:include fx:id="topMenuButtons" source="TopMenuButtons.fxml" />
Then I try to load my MainController using FXMLLoader with Guice Controller Factory. TopMenuButtons are initialized automatically because it's included to MainController.
Finally I try to inject MainController into TopMenuButtonsController (because buttons control what to display in MainController:
public class TopMenuButtonsController {
private MainController mainController;
@Inject
public void setMainController(MainController mainController) {
this.mainController = mainController;
}
@FXML
public void onCreateOrder(ActionEvent event) {
mainController.setCenter(MainController.CREATE_ORDER_FXML);
}
It succeeds but the instance injected here has all field set to null (@FXML annotation didn't work). I also see that this instance injected here is some different one than I use (different object id)
I can simply make it work by doing this in MainController:
@FXML
private void initialize() {
topMenuButtonsController.setMainController(this);
}
but my intention was to get rid of such things and use DI. What I can do?