My case is pretty complicated, and connected with this post:
How to pass variable from controller code to fxml view?
I have following .fxml views:
inclusionsSettings.fxml with no controller and no root
<fx:define>
<Label fx:id="inclusionsPrefix" />
</fx:define>
<Label text="${inclusionsPrefix.text + ' inclusions type:'}" />
<Label text="${inclusionsPrefix.text + ' inclusions size:'}" GridPane.rowIndex="1" />
<Label text="${inclusionsPrefix.text + ' inclusions number:'}" GridPane.rowIndex="2" />
initialInclusionsSettings.fxml with InitialInclusionsSettingsControl as Controller and Root
<fx:root type="javafx.scene.layout.HBox" xmlns="http://javafx.com/javafx/8.0.102" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="pl.mielecmichal.grain.cellular.automata.gui.controls.InitialInclusionsSettingsControl">
<fx:include source="inclusionsSettings.fxml"/>
</fx:root>
All *Control classes are Spring @Components
InitialInclusionsSettingsControl.java:
@Component
public class InitialInclusionsSettingsControl extends InclusionsSettingsControl {
private final SpringIntegratingUtility utility;
@Autowired
public InitialInclusionsSettingsControl(SpringIntegratingUtility utility) {
super("Initial");
this.utility = utility;
}
@PostConstruct
public void init() {
utility.loadFromFxml("views/initialInclusionsSettings.fxml", this);
}
@FXML
public void initialize(){
inclusionsPrefix.setText("Initial");
}
}
InclusionsSettingsControl.java:
public class InclusionsSettingsControl extends HBox {
private final String prefix;
@FXML
public Label inclusionsPrefix;
public InclusionsSettingsControl(String prefix) {
this.prefix = prefix;
}
@FXML
public void initialize(){
inclusionsPrefix.setText(prefix);
}
}
SpringIntegratingUtility is a FxmlLoader wrapper which provides Spring's controller factory, and set given this reference as root.
FLOW (how it is all conected):
- Spring Context is created,
- All *Control beans are created,
- Each of *Control bean creates view using SpringIntegratingUtility,
- Each .fxml view has fx:controller attribute, and during creation asks Spring for proper controller (*Control in my case) by provided factory.
WHAT I TRY TO ACHIEVE:
I would like to have base inclusionSettings.fxml included in edgeInclusionSettings.fxml, and initialInclusionsSettings.fxml, and after that two [ Edge | Initial ]Controls extending base InclusionsSettingsControl and setting fx:defined prefix for labels "Initial" or "Edge"
I could not create 2 Initial and Edge Control creating the same initialSettings.fxml because controllers are provided by Class and ther is no way for Spring to distinct which bean i would like to take.
PROBLEM and QUESTION:
Everything works fine, but during creation of InitialInclusionSettingsControl, inclusionsPrefix field (which is located in super class InclusionSettingsControl) is not injecting. I get NPE in initialize() method of base InclusionSettingsControl. (Which is btw executed)
I checked under debbuger, and FxmlLoader detects super class fields correctly.