0

我正在删除我的 JTable 中的一行并且我有这个异常:IndexOutOfBoundsException:Index:1,Size:1”。

这是我的 TableModel 类:

public Class MyTableModel extends AbstractTableModel {
private static final long serialVersionUID = -912060609250881296L; 
private ResultSet rs; 
private int rowCount; 
private int columnCount; 
private ArrayList<Object[]> data=new ArrayList<Object[]>(); 
private ArrayList<TableModelListener> listeners = new ArrayList<TableModelListener>();  

public TableModelMicrosite(ResultSet _rs) throws Exception 
{  
    setRS(_rs); 
} 

public void setRS(ResultSet _rs) 
        throws Exception 
        { 
    this.rs=_rs; 
    ResultSetMetaData metaData=_rs.getMetaData(); 
    rowCount=0; 
    columnCount=metaData.getColumnCount(); 
    while(_rs.next()){ 
        Object[] row=new Object[columnCount]; 
        for(int j=0;j<columnCount;j++){ 
            row[j]=_rs.getObject(j+1); 
        } 
        data.add(row); 
        rowCount++; 
    } 
        } 
public boolean isCellEditable(int rowIndex, int columnIndex){ 
    return false; 
} 
public void addTableModelListener(TableModelListener l)   
{   
    if (listeners.contains(l))   
        return;   
    listeners.add(l);   
}   


public void setValueAt(Object value, int rowIndex, int columnIndex)   
{   
    data.get(rowIndex)[columnIndex] = value;  

    TableModelEvent tabModEvent = new TableModelEvent(this, rowIndex,   
            rowIndex, columnIndex, TableModelEvent.UPDATE);    
    for (TableModelListener l: listeners)   
        l.tableChanged(tabModEvent);   
    fireTableCellUpdated(rowIndex, columnIndex); 
    System.out.println("Changed :  " + value); 
}   

public int getColumnCount(){ 
    return columnCount; 
} 

public int getRowCount(){ 
    return rowCount; 
} 

public Object getValueAt(int rowIndex, int columnIndex){ 
     Object[] row=(Object[]) data.get(rowIndex); 
    System.out.println("rowIndex : "+rowIndex);     System.out.println("columnIndex : "+columnIndex);  
    return row[columnIndex]; 
     // return data[rowIndex][columnIndex]; 

} 

public String getColumnName(int columnIndex){ 
    try{ 
        ResultSetMetaData metaData=rs.getMetaData(); 
        return metaData.getColumnName(columnIndex+1); 
    }catch(Exception e){ 
        e.printStackTrace(); 
        return null; 
    } 
}    

public void addRow(Object[] row) { 
    data.add(0, row); 
    fireTableRowsInserted(0, 0); 
} 

public void removeRow(int  selectedrow) {   
        data.remove(selectedrow) ; 
             fireTableRowsDeleted(selectedrow, selectedrow);   
} 

}

这就是我在主体类中调用 removeRow() 方法的方式:

table = new JTable(monDataModel()); 
int [] row = table.getSelectedRows();  
table.clearSelection(); 

   List<Integer> selectedModelRows = new LinkedList<Integer>(); 
   for (int i = 0; i < row.length; i++) { 
       selectedModelRows.add(table.convertRowIndexToModel(row[i])); 
   } 
   Collections.sort(selectedModelRows, Collections.reverseOrder()); 

   for (int selectedModelRow : selectedModelRows) { 
       ((TableModelMicrosite) table.getModel()).removeRow(selectedModelRow); 
       ((TableModelMicrosite) table.getModel()).fireTableRowsDeleted(selectedModelRow, selectedModelRow); 
   }

我认为 TableModel 中的 getValue() 方法存在问题,但我不知道是什么错误。

你能帮我吗 ?非常感谢 !

4

1 回答 1

0

这就是我正在尝试的,但我仍然是“IndexOutOfBoundsException”异常

公共无效删除RowFromTable()

{

//myTable = new JTable(myTableModel);

int [] 行 = myTable.getSelectedRows();

for(int i = 0; i < rows.length; i++) {

int 行 = 行 [i];

int modelRow = myTable.convertRowIndexToModel(rows);

myTable.getModel().removeRow(modelRow);

}

这是我在 tableModel 中的删除方法:

公共无效删除行(int selectedrow)

{

data.remove(selectedrow) ;

fireTableRowsDeleted(selectedrow, selectedrow);

}

于 2013-05-17T09:05:25.317 回答