我用JavaFX中的TableView
制作了一个可编辑表。现在,我想检查输入是否正确。如果不正确,我想将单元格的值重置为旧值。
就像我将单元格中的“ 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”才会重新出现。
答案 0 :(得分:0)
答案是方法refresh():
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 {
error_message_group.textProperty().set("to old");
table_group.refresh();
}
}
答案 1 :(得分:-1)
上面帖子中的刷新呼叫:
table_group.refresh();
需要用新值或旧值更新表。
这是我处理单元格中双精度值的方法:
@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;
}