添加TableRowSorter后,向模型中添加值会导致java.lang.IndexOutOfBoundsException:无效范围 [英] After adding a TableRowSorter adding values to model cause java.lang.IndexOutOfBoundsException: Invalid range

查看:149
本文介绍了添加TableRowSorter后,向模型中添加值会导致java.lang.IndexOutOfBoundsException:无效范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将TableRowSorter添加到表及其对应的模型之后,任何相应的添加都专门在firetabletablerowsinsertted原因异常中添加.通过测试很明显,GetRowCount()返回的值超出了模型范围.但是,对我而言,在添加排序器或过滤器后如何继续向表中添加值没有意义?

After adding a TableRowSorter to a table and its corresponding model any corresponding adds specifically at firetabletablerowsinserted cause exceptions. It is clear from testing that the GetRowCount() is returning a value past the models range. However it does not make sense to me how to continue to add values to the table after a sorter or filter has been added?

作为示例,我在向表中添加任何内容之前设置了行过滤器,然后在模型中通过以下调用向表中添加了一个值:

As an example, I set the row filter before adding anything to the table then add a value to the table with the following calls in my model:

this.addRow(row, createRow(trans,row));
this.fireTableRowsInserted(this.getRowCount(), this.getRowCount());

行数的大小为1,并引发异常:

The rowcount is of size 1 and the exception is thrown:

java.lang.IndexOutOfBoundsException: Invalid range
at javax.swing.DefaultRowSorter.checkAgainstModel(Unknown Source)
at javax.swing.DefaultRowSorter.rowsInserted(Unknown Source)
at com.gui.model

如果我执行相同的步骤而没有先添加排序器,则一切都很好.我以为我可能需要通知模型排序器可能已进行更改,并尝试了以下操作,但仍返回异常:

If I do the same steps without first adding the sorter everything is fine. I assumed that possibly I needed to notify the model that the sorter may have made changes and tried the following but still returns an exception:

this.addRow(row, createRow(trans,row));
this.fireTableStructureChanged()
this.fireTableRowsInserted(this.getRowCount(), this.getRowCount());

我什至尝试在模型内部通知排序器在调用如下所示的fire之前已向模型中添加了一个值,但它也失败了:

I even tried to notify the sorter inside the model that a value has been added to the model before calling fire like below but it fails as well:

 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中设置排序器的代码:

Lastly, I hard coded the FireTableRowsInsterted(0,0) and it does not throw any exception. But nothing gets added to table? So, I know it is definitely some type of OutOfBounds issue. I have looked all over and cannot seem to find the answer. If anyone has any idea how this is suppose to work it be very helpful. Here is code that sets the sorter inside 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);

这是我模型中的代码,可为模型增加价值:

Here is code inside my model that adds value to model:

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());

                    }

推荐答案

您遇到out by 1错误.触发事件的正确代码是:

You have an out by 1 error. The correct code for firing the event is:

this.fireTableRowsInserted(this.getRowCount()-1, this.getRowCount()-1);

这篇关于添加TableRowSorter后,向模型中添加值会导致java.lang.IndexOutOfBoundsException:无效范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆