简体   繁体   English

在JTable中删除一行后如何停止编辑单元格

[英]How to stop editing cell after removing a row in JTable

In my JTable when a new row is added, automatically a "JTextString" cell is selected to be edited. 在我的JTable中,当添加新行时,将自动选择一个“ JTextString”单元格进行编辑。 If the new value entered in the cell fails the validation, this new row is supposed to be removed. 如果在单元格中输入的新值未通过验证,则应删除此新行。

In my DefacultCellEditor instance I have: 在我的DefacultCellEditor实例中,我有:

        @Override
        public boolean stopCellEditing(){
            /* do something */

                if (/* failed validation */) {
                    super.stopCellEditing();
                    myTable.getSelectionModel().clearSelection();
                    myTable.removeLastRow();
                    return true;
                }
           ....
         }

Actually when the validation fails, the last row is removed: 实际上,当验证失败时,最后一行将被删除:

    model.removeRow(model.getRowCount() - 1);

but the same cell in the above row is editing. 但上一行的同一单元格正在编辑。 How can I stop the editing and leaving all the rows deselected? 如何停止编辑并取消选择所有行?

It would be helpful if you posted a sample of code that could be run directly. 如果您发布了可以直接运行的代码示例,将很有帮助。 Since you didn't, I haven't tested this out - I have no idea what other changes you've made in your table model. 既然您没有,我还没有测试过-我不知道您在表模型中做了哪些其他更改。

Change 更改

super.stopCellEditing();

to

super.cancelCellEditing();

and remove the line with clearSelection . 并使用clearSelection删除该行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM