Injecting FXML into a derived FX Class(Controller) from the base class works - but why?
The code below is actually working. But i am curious why?
The FXML is loaded in the constructor of the abstract Base class (FXMLPopup) and injected to the derived class(TestfxmlController).
My problem: when the base class is constructed(and the fxml gets injected) the derived class hasn't been constructed yet. Also imho the base shouldn't know anything about the derived class, should it?
Further the field, that is to be injected is private in the derived class! So the loader has to make it accessible but there is no @FXML in the base that would give permision to do so(permision is given only in the derived class that hasn't yet been constructed - Well the Field doesn't exist at all in the base!).
Still the FXML gets correctly injected into the derived class - and the fields are actually the ones in the derived class. Why does this work?
Baseclass:
public abstract class FXMLPopup extends Popup implements Initializable {
@SuppressWarnings("LeakingThisInConstructor")
public FXMLPopup(String filename) {
super();
final FXMLLoader loader = new FXMLLoader(FXMLLoader.class.getResource(filename));
//if a controller is set in the fxml, ignor it.
loader.setControllerFactory(p -> this);
try {
this.getContent().add(loader.load());
} catch (IOException ex) { }
}
}
Derived class:
public class TestfxmlController extends FXMLPopup {
@FXML
private ChoiceBox<String> testChoiceBox;
public TestfxmlController() {
super("fxml/testfxml.fxml");
}
@Override
public void initialize(URL url, ResourceBundle rb) {
//works!!!!
testChoiceBox.getItems().add("test");
}
}
FXMLCode:
<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/11.0.1" fx:controller="TestfxmlController">
<children>
<ChoiceBox fx:id="testChoiceBox" layoutX="113.0" layoutY="160.0" prefWidth="150.0" />
</children>
</AnchorPane>
What would I expect? I would expect Errors over errors. That the loader doesn't find the fields in the base class and that access is denied.... But it somehow magicaly works flawless. Despite I am violating like everything possible in such a small example. I'd like to understand the "magic" behind this...