I'm trying to delete all entrys from my abstractTableModel. As long as I don't delete the last remaining row, everything works fine, but as soon as I delete this one, I get an ArrayOutOfBoundsException. I'm using a DefaultRowSorter and this seems to be the Exception.
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: Array index out of 开发者_运维知识库range: 0 at java.util.Vector.get(Vector.java:694) at graphics.tableModel.MyTableModel.getValueAt(MyTableModel.java:78) at graphics.tableModel.MyTableModel.getColumnClass(MyTableModel.java:90) at javax.swing.table.TableRowSorter.useToString(TableRowSorter.java:224) at javax.swing.DefaultRowSorter.updateUseToString(DefaultRowSorter.java:607) at javax.swing.DefaultRowSorter.sort(DefaultRowSorter.java:556) at javax.swing.DefaultRowSorter.shouldOptimizeChange(DefaultRowSorter.java:1008) at javax.swing.DefaultRowSorter.rowsDeleted(DefaultRowSorter.java:866) at javax.swing.JTable.notifySorter(JTable.java:4262) at javax.swing.JTable.sortedTableChanged(JTable.java:4106) at javax.swing.JTable.tableChanged(JTable.java:4383) at javax.swing.table.AbstractTableModel.fireTableChanged(AbstractTableModel.java:280)
my Code to delete all Rows:
public void deleteAll() {
int size = data.size()-1;
data.clear();
this.fireTableRowsDeleted(0, size);
}
Same thing happens with simply deleting the last existing row.
public void deleteRow(int row) {
data.remove(row);
}
the way i'm calling deleteRow:
for (int i = rows.length - 1; i >=0; i--) {
tm.deleteRow(rows[i]); }
tm.fireTableDataChanged();
thanks for your help
It seems that the problem is in MyTableModel which you use. The model's getColumnClass() tries to invoke getValueAt() in order to determine the type, but there are no values in the table, thus the exception. So just fix getColumnClass() so that it doesn't invoke getValueAt(). Normally, the column types are not changing, so you should have something like this:
public Class<?> getColumnClass(int columnIndex) {
switch (columnIndex) {
case 0: return Integer.class;
case 1: return String.class;
case 2: return Double.class;
default: return null;
}
}
The exception is coming out of your code:
graphics.tableModel.MyTableModel.getValueAt(MyTableModel.java:78)
Looks like your implementation of getValueAt needs to be updated to handle locations that don't exist in the table?
You'd also benefit from overriding getcolumnclass so that it doesn't use its somewhat hacky grab the first row and see what's there method to begin with :)
精彩评论