我的情况非常复杂,并且与这篇文章有关:
How to pass variable from controller code to fxml view?
我有以下.fxml观看次数:
没有控制器且没有根的inclusionsSettings.fxml
<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 , InitialInclusionsSettingsControl 作为控制器和根
<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>
所有 * Control 类都是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 是一个FxmlLoader包装器,提供Spring的控制器工厂,并以root身份设置此引用。
FLOW(如何全部连接):
我尝试实现的目标:
我希望 edgeInclusionSettings.fxml 和 initialInclusionsSettings.fxml 中包含 inclusionSettings.fxml ,之后 > [边缘|初始]控制扩展基础InclusionsSettingsControl和设置fx:标签“Initial”或“Edge”的已定义前缀
我无法创建2个Initial和Edge Control创建相同的initialSettings.fxml,因为控制器是由Class提供的,因此Spring无法区分我想要使用的bean。
问题和问题:
一切正常,但在创建 InitialInclusionSettingsControl 期间,inclusionsPrefix字段(位于超类 InclusionSettingsControl 中)不会注入。我在base InclusionSettingsControl 的initialize()方法中得到了NPE。 (这是顺便说一句)
我在debbuger下检查过,而FxmlLoader正确检测到了超类字段。