添加一個TableRowSorter到表和任何相應的其相應的模型之後,在firetabletablerowsinserted原因的異常增加特異性。從測試中可以明顯看出,GetRowCount()正在返回超出模型範圍的值。然而,它沒有任何意義,我該如何繼續使用已加入分類器或過濾後的值添加到表?添加一個TableRowSorter添加值以引起java.lang.IndexOutOfBoundsException模型後:無效範圍
舉個例子,我設置行過濾器添加任何表在此之前的值添加到表在我的模型以下呼叫:
this.addRow(row, createRow(trans,row));
this.fireTableRowsInserted(this.getRowCount(), this.getRowCount());
行數是1型和異常的拋出:
java.lang.IndexOutOfBoundsException: Invalid range
at javax.swing.DefaultRowSorter.checkAgainstModel(Unknown Source)
at javax.swing.DefaultRowSorter.rowsInserted(Unknown Source)
at com.gui.model
如果我做同樣的步驟,如果沒有事先添加分揀機一切正常。我以爲,可能我需要通知分揀機可能已經進行了更改,並試圖模型以下,但仍然會返回一個例外:
this.addRow(row, createRow(trans,row));
this.fireTableStructureChanged()
this.fireTableRowsInserted(this.getRowCount(), this.getRowCount());
我甚至試圖通知值已增加了模型內部分揀機調用火象下面前的模式,但它也將失敗:
this.addRow(row, createRow(trans,row));
if(sorter.getRowFilter() != null){
//if a sorter exists we are in add notify sorter
sorter.rowsInserted(getRowCount(), getRowCount());
}
this.fireTableRowsInserted(this.getRowCount(), this.getRowCount());
最後,我硬編碼的FireTableRowsInsterted(0,0),它不拋出任何異常。但沒有添加到表中?所以,我知道這絕對是某種類型的OutOfBounds問題。 我到處都找過,似乎無法找到答案。如果任何人有任何想法如何工作這是非常有益的。 這裏是代碼,設置裏面的JPanel分揀機:
messageTable.setRowSorter(null);
HttpTransactionTableModel m = getTransactionTableModel();
final int statusIndex = m.getColIndex("status");
RowFilter<Object,Object> startsWithAFilter = new RowFilter<Object,Object>() {
public boolean include(Entry<? extends Object, ? extends Object> entry) {
for(char responseCode:responseCodes)
{
if (entry.getStringValue(statusIndex).startsWith(Character.toString(responseCode))) {
return true;
}
}
// None of the columns start with "a"; return false so that this
// entry is not shown
return false;
}
};
m.sorter.setRowFilter(startsWithAFilter);
messageTable.setRowSorter(m.sorter);
這裏是我的模型內部代碼附加價值的模型:
public void update(Observable o, Object evt) {
if (evt instanceof ObservableEvent<?>) {
ObservableEvent<?> event = (ObservableEvent<?>) evt;
if (event.getElement() instanceof HttpTransaction) {
HttpTransaction trans = (HttpTransaction) event.getElement();
// handle adding of an element
if (event.getAction() == PUT) {
if (includeTransaction(trans)) {
// handle request elements
if (trans.getRequest() != null && idMap.get(trans.getID()) == null) {
idMap.put(trans.getID(), count++);
// transactionManager.save(trans);
int row = idMap.get(trans.getID());
this.addRow(row, createRow(trans,row));
if(sorter.getRowFilter() != null){
sorter.rowsInserted(getRowCount(), getRowCount());
}
this.fireTableRowsInserted(this.getRowCount(), this.getRowCount());
}
'this.fireTableRowsInserted(this.getRowCount(),this.getRowCount());' - 關閉的一個,它的零基指數 – Whired 2012-06-04 18:21:26
+1 @Whired - 好斑點:-) – kleopatra 2012-08-17 15:19:44