我TableView
在JavaFX中创建了一个可编辑的表。现在我想检查输入是否正确。如果错了,我想将单元格的值重置为旧值。就像我在单元格中编辑“55”到“160”。我想按下之后Enter,“160”消失了。我这样做了:
public void handle(CellEditEvent event) {
if (groups_list.get(table_persona.getSelectionModel().getSelectedIndex()).getAge() > 100) {
Number a = (Number) event.getNewValue();
((Persona) event.getTableView().getItems().get(
event.getTablePosition().getRow()
)).setAge(a.intValue());
} else {
Number a = groups_list.get(table_persona.getSelectionModel().getSelectedIndex()).getAge();
((Persona) event.getTableView().getItems().get(
event.getTablePosition().getRow()
)).setAge(a.intValue());
error_message_group.textProperty().set("to old");
}
table_groups.setItems(persona_list);
}
但它不起作用。“160”仍在那里。只有当我再次单击单元格时,才会再次出现“55”。
发布于 2019-02-19 13:42:52
对上面帖子中的补充,请调用:
table_group.refresh();
需要使用新值或旧值更新表。
以下是我在单元格中检查double值的方法:
@FXML
private void changeDiameter(CellEditEvent<?, ?> edittedCell) {
// get the selected item in the diameter column
MillToolTable toolSelected = toolTable.getSelectionModel().getSelectedItem();
try {
// force an error if the value cannot be converted to a double value
Double temp = Double.parseDouble(edittedCell.getNewValue().toString());
// make sure the number is positive
if (temp < 0) {
// display an alert message to tell the user they have entered an invalid diameter
Alert alert = new Alert(AlertType.WARNING);
// set the title of the window
alert.setTitle("Invalid tool diameter");
// eliminate the header text area from the window
alert.setHeaderText(null);
// display the following message
alert.setContentText("Please enter a positive number for the tool's diameter.");
// get the stage variable for the alert window
Stage stage = (Stage) alert.getDialogPane().getScene().getWindow();
// make is display on top of open windows
stage.setAlwaysOnTop(true);
// finally show the alert window
alert.show();
// refresh the table to display the old value
toolTable.refresh();
// exit this method and do not set the tool length property to the updated value
return;
}
} catch (Exception e) {
// display an alert message to tell the user they have entered an invalid diameter
Alert alert = new Alert(AlertType.WARNING);
// set the title of the window
alert.setTitle("Invalid tool diameter");
// eliminate the header text area from the window
alert.setHeaderText(null);
// display the following message
alert.setContentText("Please enter a positive number for the tool's diameter.");
// get the stage variable for the alert window
Stage stage = (Stage) alert.getDialogPane().getScene().getWindow();
// make is display on top of open windows
stage.setAlwaysOnTop(true);
// finally show the alert window
alert.show();
// remove the invalid data from the table and load the old, valid value
toolTable.refresh();
// exit this method and do not set the tool length property to the updated value
return;
}
https://stackoverflow.com/questions/-100008954
复制相似问题