首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何检查javafx tableview中的编辑单元格是否正确,如果不正确:如何设置单元格值?

如何检查javafx tableview中的编辑单元格是否正确,如果不正确:如何设置单元格值?
EN

Stack Overflow用户
提问于 2019-02-19 04:11:08
回答 2查看 0关注 0票数 0

TableView在JavaFX中创建了一个可编辑的表。现在我想检查输入是否正确。如果错了,我想将单元格的值重置为旧值。就像我在单元格中编辑“55”到“160”。我想按下之后Enter,“160”消失了。我这样做了:

代码语言:javascript
复制
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”。

EN

Stack Overflow用户

发布于 2019-02-19 13:42:52

对上面帖子中的补充,请调用:

table_group.refresh();

需要使用新值或旧值更新表。

以下是我在单元格中检查double值的方法:

代码语言:javascript
复制
@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;
    }       
票数 0
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100008954

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档