删除最后一行Jtable时出错 [英] Error deleting last row Jtable
问题描述
当我尝试删除Jtble的最后一行时,会抛出OutBound错误.
When i try to delete the last row in Jtble it throws me an OutBound error.
此处实现Jtable&的代码默认表:
here the code that implements the Jtable & DefaultTable:
Vector<String> rowOne = new Vector<>();
rowOne.addElement("Harry");
rowOne.addElement("100414");
rowOne.addElement("21");
rowOne.addElement("239438");
rowOne.addElement("24/24/23");
rowOne.addElement("30000");
Vector<String> rowTwo = new Vector<>();
rowTwo.addElement("Gordon");
rowTwo.addElement("34353");
rowTwo.addElement("25");
rowTwo.addElement("2538");
rowTwo.addElement("24/24/23");
rowTwo.addElement("20000");
Vector<Vector> rowData = new Vector<>();
rowData.addElement(rowOne);
rowData.addElement(rowTwo);
columnNames = new Vector<>();
columnNames.addElement("Name");
columnNames.addElement("Cc");
columnNames.addElement("Age");
columnNames.addElement("Phone");
columnNames.addElement("Date");
columnNames.addElement("Amount");
DefaultTableModel model = new DefaultTableModel(rowData, columnNames);
Jtable table = new JTable(model);
以下是删除代码:
else if (e.getActionCommand().equals("deleteClient"))
{
if(table.getSelectedRow() != -1)
{
DefaultTableModel tModel1 = (DefaultTableModel) table.getModel();
int seletedRow = table.getSelectedRow();
tModel1.removeRow(seletedRow);
}
仅在删除最后一个Jtable的行时会引发错误,当我删除第一行或中间的另一行时,不会引发任何错误,我该如何解决呢?
The error is thrown just when deleting the last Jtable's row, when I delete a diferent row as first one or a middle one, no error is thrown, how can i solve it?
推荐答案
您的问题不在于线路
tModel1.removeRow(seletecdRow);
但是
int selectedRow = table.getSelectedRow();
int selectedCol = table.getSelectedColumn();
tModel1.removeRow(selectedRow);
String name = (String) tModel1.getValueAt(seletecdRow, seletecdCol); // Here
删除最后一行时,存储在selectedRow
中的索引不再有效.此外,每当删除一行时,它都会被取消选择,因此table.getSelectedRow()
将返回-1(因为未选择任何行).
When you delete the last row, the index you store in selectedRow
is no longer valid. Moreover whenever you delete a row, it gets de-selected and so, table.getSelectedRow()
will return -1 (as no row is selected).
假设您有5行:
您可以选择第1行到第4行(索引0到3)中的任何行并将其删除. selectedRow = 0,1,2 or 3
,您剩下4行,因此索引0、1、2或3处仍存在一行,并且model.getValueAt(selectedRow...
起作用
You can select any row from row 1 to row 4 (index 0 to 3) and delete it. selectedRow = 0,1,2 or 3
and you're left with 4 rows so there still exists a row at index 0, 1, 2 or 3. and model.getValueAt(selectedRow...
works
但是,如果您选择第5行(最后一行,索引4)并将其删除. selectedRow = 4
,您剩下4行(索引0、1、2、3),但索引4不再有一行.model.getValueAt(selectedRow...
给出了错误
But if you select row 5 (last row, at index 4) and delete it. selectedRow = 4
and you're left with 4 rows (index 0, 1, 2, 3) but there is no longer a row at index 4. and model.getValueAt(selectedRow...
gives an error
解决方案取决于您删除行后在标签中要执行的操作.
The solution depends on what you're trying to do in the label after deleting the row.
-
如果要设置标签以显示已删除行的信息:
If you're trying to set the label to display the info of the deleted row:
String name = (String) tModel1.getValueAt(table.getSelectedRow(), table.getSelectedColumn());
labelStatus.setText(name);
tModel1.removeRow(table.getSelectedRow());
如果要重新选择表中的另一行并设置标签以显示此新选择的行的信息,则:
If you're trying to re-select another row in the table and set the label to display the info of this newly selected row:
int selectedRow = table.getSelectedRow();
tModel1.removeRow(selectedRow);
int numRowsLeft = tModel1.getRowCount();
if (numRowsLeft > 0) { // only reset selection if table is not empty
if (selectedRow > numRowsLeft - 1) { // last row deleted, new selection will be new last row
table.setRowSelectionInterval(numRowsLeft-1, numRowsLeft-1);
} else { // deleted from the start or middle, selectedRow now points to the next row after the deleted row
table.setRowSelectionInterval(selectedRow, selectedRow);
}
String name = (String) tModel1.getValueAt(table.getSelectedRow(), table.getSelectedColumn());
labelStatus.setText(name);
}
这篇关于删除最后一行Jtable时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!