将TableRowSorter添加到表及其对应的模型后,任何相应的添加都会特别添加firetabletablerowsinserted导致异常。从测试中可以清楚地看出,GetRowCount()返回的值超出了模型范围。但是,添加分拣机或过滤器后,如何继续向表中添加值对我没有意义?
例如,我在向表中添加任何内容之前设置行过滤器,然后在我的模型中使用以下调用向表中添加值:
this.addRow(row, createRow(trans,row));
this.fireTableRowsInserted(this.getRowCount(), this.getRowCount());
rowcount的大小为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());
我甚至尝试通知模型内部的分拣机在调用fire之前已经将一个值添加到模型中,但是它也失败了:
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());
}
答案 0 :(得分:3)
你有1个错误。触发事件的正确代码是:
this.fireTableRowsInserted(this.getRowCount()-1, this.getRowCount()-1);
答案 1 :(得分:2)
在看到kleopatra的评论之后,我回过头来看看这个。我在创建RowSorter之后更改了我的TableModel,但在将RowSorter附加到JTable之前。这是一个显示我遇到的问题的例子。
import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableRowSorter;
import java.util.ArrayList;
import java.util.List;
public class TestTableMain {
public static void main(String[] args) {
new TestTableMain();
}
public TestTableMain() {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
buildAndShowMainFrame();
}
});
}
private void buildAndShowMainFrame() {
JFrame frame = new JFrame();
JScrollPane scrollPane = new JScrollPane();
TestTableModel model = new TestTableModel();
JTable table = new JTable(model);
TableRowSorter<TestTableModel> rowSorter = new TableRowSorter<>(model);
rowSorter.setRowFilter(null);
model.add("First added item.");
/* The RowSorter doesn't observe the TableModel directly. Instead,
* the JTable observes the TableModel and notifies the RowSorter
* about changes. At this point, the RowSorter(s) internal variable
* modelRowCount is incorrect. There are two easy ways to fix this:
*
* 1. Don't add data to the model until the RowSorter has been
* attached to the JTable.
*
* 2. Notify the RowSorter about model changes just prior to
* attaching it to the JTable.
*/
// Uncomment the next line to notify rowSorter that you've changed
// the model it's using prior to attaching it to the table.
//rowSorter.modelStructureChanged();
table.setRowSorter(rowSorter);
scrollPane.setViewportView(table);
frame.setContentPane(scrollPane);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
model.add("Second added item.");
}
private class TestTableModel extends AbstractTableModel {
private List<String> items = new ArrayList<>();
public TestTableModel() {
for(int i=0;i<5;i++) {
add("Item " + i);
}
}
@Override
public int getRowCount() {
return items.size();
}
@Override
public int getColumnCount() {
return 1;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
return items.get(rowIndex);
}
public void add(String item) {
items.add(item);
fireTableRowsInserted(items.size() - 1, items.size() - 1);
}
}
}
答案 2 :(得分:0)
所以,现在看来,如果你检查你的模型,如果你当前处于排序模式,如果是这种情况,只需要在排序模型上调用更新。否则调用普通模型火灾更新一切似乎到目前为止工作。我仍然愿意接受更好的方法来解决这个问题:
if(sorter.getRowFilter() != null){
sorter.modelStructureChanged();
}
else
this.fireTableRowsInserted(this.getRowCount(), this.getRowCount());