我的应用程序的结构大致类似于MainApplication-> SuperFXMLController-> ChildFXMLController。每个ChildFXML都有一个按钮,可以在SuperFXML中向GridPane添加一个子元素我已经在ChildFXMLController中定义了buttonHandler方法,但是动作本身应该在SuperFXMLController中添加(在网格中添加新行)。问题是如何处理SuperFXMLController的动作?
这里有一些代码细节: AddLogRecordController.java(所谓的“SuperFXMLController”):
@FXML
private void initialize() throws IOException {
rowIndex = 0;
FXMLLoader loader = new FXMLLoader(getClass().getResource("RecordGrid.fxml"));
mainGrid.add(loader.load(), 0, rowIndex++);
RecordGridController controller = (RecordGridController) loader.getController();
controller.setSuperController(this);
}
...
public void addRowHandle() {
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("RecordGrid.fxml"));
mainGrid.getChildren().add(loader.load());
RecordGridController controller = loader.getController();
} catch (IOException e) {
e.printStackTrace();
}
}
“RecordGridController”称为“ChildFXMLController”:
private AddLogRecordController superController;
...
public void setSuperController(AddLogRecordController addLogRecordControllerClass) {
this.superController = addLogRecordControllerClass;
}
...
@FXML
private void addRowHandler() {
superController.addRowHandle();
}
答案 0 :(得分:0)
好的,现在我按计划做了它应该做的事情。我在SuperController中添加了以下方法:
private void loadRecordGrid(int rowIndex) {
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("RecordGrid.fxml"));
mainGrid.add(loader.load(), 0, rowIndex);
RecordGridController controller = (RecordGridController) loader.getController();
controller.initialize(this);
} catch (IOException e) {
e.printStackTrace();
}
}
然后方法初始化如下:
@FXML
private void initialize() {
rowIndex = 0;
loadRecordGrid(rowIndex++);
}
方法addRowHandler如下:
public void addRowHandle() {
loadRecordGrid(rowIndex++);
}
在ChildController中:
private AddLogRecordController superController;
@FXML
public void initialize(AddLogRecordController superController) {
this.superController = superController;
}
@FXML
private void addRowHandler() {
superController.addRowHandle();
}
不知道,也许它可以以更有效的方式完成,但现在可行。 现在我将需要计算如何将textfields的值从child传递给parent并返回