更新时间:2023-12-06 15:53:34
addPart.fxml
中的fx:id="partTable"
中没有元素.因此,partTable
为空,并且
The is no element in addPart.fxml
with fx:id="partTable"
. Consequently partTable
is null, and
partTable.getItems();
引发空指针异常.
您需要将partTable
注入到定义了它的FXML的控制器中:
You need to inject partTable
into the controller for the FXML in which it is defined:
public class FXMLDocumentController implements Initializable {
@FXML
private Label label;
@FXML
private TableView<Inhouse> partTable ;
// ...
}
AddPartController
仅需要访问与表关联的项目列表,因此您可以为其定义一个字段以及用于初始化它的方法:
The AddPartController
only needs access to the list of items associated with the table, so you can define a field for it, and a method for initializing it:
public class AddPartController implements Initializable {
// ...
private ObservableList<Inhouse> tableItems ;
public void setTableItems(ObservableList<Inhouse> tableItems) {
this.tableItems = tableItems ;
}
// ...
}
然后在加载addPart.fxml
时设置项目:
Then set the items when you load addPart.fxml
:
@FXML
private void addPart(ActionEvent event) throws IOException {
FXMLLoader loader = new FXMLLoader(getClass().getResource("addPart.fxml"));
Parent add_part_parent = loader.load();
AddPartController addPartController = loader.getController();
addPartController.setTableItems(partTable.getItems());
Scene add_part_scene = new Scene(add_part_parent);
add_part_scene.getStylesheets().add("style.css");
Stage app_stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
app_stage.setScene(add_part_scene);
app_stage.show();
}
然后您当然只需要
@FXML
public void addInhouse(ActionEvent event){
tableItems.add(new Inhouse(partNameField.getText(),
Integer.parseInt(partInstockField.getText()),
Double.parseDouble(partPriceField.getText()),
Integer.parseInt(partMaxField.getText()),
Integer.parseInt(partMinField.getText()),
Integer.parseInt(inhouseTextField.getText())
// Integer.parseInt(outsourcedTextField.getText())
));
}
(FWIW我不知道是什么
(FWIW I have no idea what
partInstockField.setText(String.valueOf(partInstockField));
等应该做的.)