问题
I'm removing a row in my JTable and i have this Exception : IndexOutOfBoundsException: Index: 1, Size: 1".
This is my TableModel Classe:
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);
}
}
And this is how i call my removeRow() method in my principal Class :
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);
}
I think there is a issue with the getValue() method in the TableModel, but i don't know what is the mistake.
Can you help me please ? Thanks a lot !
回答1:
This is what i'm trying, but i still the "IndexOutOfBoundsException" Exception
public void removingRowFromTable()
{
//myTable = new JTable(myTableModel);
int [] rows = myTable.getSelectedRows();
for(int i = 0; i < rows.length; i++) {
int row = rows[i];
int modelRow = myTable.convertRowIndexToModel(rows);
myTable.getModel().removeRow(modelRow);
}
This is my remove methode in the tableModel :
public void removeRow(int selectedrow)
{
data.remove(selectedrow) ;
fireTableRowsDeleted(selectedrow, selectedrow);
}
来源:https://stackoverflow.com/questions/16584092/java-jtable-removerow-java-lang-indexoutofboundsexception-index-1-size-1