JTable removeRow(),删除错误的行 [英] JTable removeRow(), removing wrong row

查看:74
本文介绍了JTable removeRow(),删除错误的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JTable,我需要删除一行,即选定的行.

I have a JTable and I need to remove a row, namely, the selected row.

所以首先,我得到表模型:

So first, I get the the table model :

    DefaultTableModel model = (DefaultTableModel) table.getModel();

然后选择的行(如果选择了第二行,则返回1,这是可以理解的,因为行从零开始):

Then the selected row (if the second row is selected, this returns 1, which is understandable because rows start from zero):

    int selectedRow = table.getSelectedRow();

然后我尝试删除该行:

    model.removeRow(selectedRow);

然后我再次设置表模型:

Then I set the table model again:

    table.setModel(model);

这实现的是删除一个完全随机的行.我根本不明白为什么.我已经使用table.setRowSorter(sorter)对表进行了排序,但是我不知道为什么这应该是一个问题.如果绝对需要SSCCE,请告诉我,因为在生产一个SSCCE之前,我有很多代码需要修改.

What this achieves is removing a completely random row. I simply can't understand why. I have sorted the table at some point, using table.setRowSorter(sorter), but I don't know why that should be a problem. If an SSCCE is absolutely needed please let me know, because I have a lot of code to modify before I can produce one.

注意:这两行返回的值不同:

    System.out.println(table.getValueAt(selectedRow, 1));
    System.out.println(model.getValueAt(selectedRow, 1));

推荐答案

如果对JTable进行了过滤或排序,则可以转换

If is JTable filtered or sorted then you can convert

int modelRow = convertRowIndexToModel(row);

这篇关于JTable removeRow(),删除错误的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆