且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Java Swing的键事件中删除JTable中的选定行

更新时间:2022-06-26 07:17:33

您必须获取选定的行(即光标当前所在的行),然后在该行上调用removeRow.

You have to get the selected Rows (thats where the curser currently is) and then call removeRow on that rows.

我建议您阅读 JTable的API

尝试一下(我在使用代码的地方在代码中使用了多行,但是您应该可以将其分解为一行.而且,我不确定Arrays.sort是否真的必要)

try this (I used multiple rows in the code where I used it, but you should be able to break it down to one. Also, I'm unsure if the Arrays.sort is really necessary)

int [] toDelete = dataTable.getSelectedRows();
Arrays.sort(toDelete); // be shure to have them in ascending order.
MyTableModel myTableModel = (MyTableModel)dataTable.getModel();
for(int ii = toDelete.length -1; ii >=0; ii--) {
    myTableModel.removeRow(toDelete[ii]); // beginning at the largest.
}