JTable の行を削除していますが、次の例外があります: IndexOutOfBoundsException: Index: 1, Size: 1".
これは私の 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);
}
}
そして、これは私がプリンシパル Class で私の 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() メソッドに問題があると思いますが、どこが間違っているのかわかりません。
私を助けてくれませんか?どうもありがとう !