0

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):

  1. Spring Context is created,
  2. All *Control beans are created,
  3. Each of *Control bean creates view using SpringIntegratingUtility,
  4. 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.

Community
  • 1
  • 1
Michał Mielec
  • 1,582
  • 1
  • 14
  • 38
  • 1
    Can you show the `SpringIntegratingUtility` (which I assume creates a `FXMLLoader` and calls it's `load()` method)? If `@FXML` injection is not working properly, it's probably something to do with the way you are setting the `controller` or `controllerFactory` on the `FXMLLoader`. – James_D Dec 14 '16 at 01:59
  • I'll show this class later, but when I simply copy and paste this fragment instead of fx:include it works. Super class fields are injected. – Michał Mielec Dec 14 '16 at 13:11
  • 1
    Included FXML files will not inject fields into the controller for the surrounding FXML: they will only inject fields into their own controllers. – James_D Dec 14 '16 at 13:17
  • Your related question is about : "FxmlLloader nameSpace for passing arguments": does it work? Also in this line,`utility.loadFromFxml("views/initialInclusionsSettings.fxml", this);` How doest "SpringIntegrationUtility" works (the source may help) . I suspect that the fxmlLoader in SpringIntegrationUtility create a new controller, – pdem Dec 22 '16 at 10:28

0 Answers0