title: After adding a TableRowSorter adding values to model cause java.lang.IndexOutOfBoundsException: Invalid range
body: <p>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? </p>
  
  <p>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:     </p>
  
  <pre><code>this.addRow(row, createRow(trans,row));
  this.fireTableRowsInserted(this.getRowCount(), this.getRowCount());
  </code></pre>
  
  <p>The rowcount is of size 1 and the exception is thrown:</p>
  
  <pre><code>java.lang.IndexOutOfBoundsException: Invalid range
  at javax.swing.DefaultRowSorter.checkAgainstModel(Unknown Source)
  at javax.swing.DefaultRowSorter.rowsInserted(Unknown Source)
  at com.gui.model
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>this.addRow(row, createRow(trans,row));
  this.fireTableStructureChanged()
  this.fireTableRowsInserted(this.getRowCount(), this.getRowCount());
  </code></pre>
  
  <p>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:</p>
  
  <pre><code> 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());
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>    messageTable.setRowSorter(null);
       HttpTransactionTableModel m = getTransactionTableModel();
       final int statusIndex = m.getColIndex("status");
       RowFilter&lt;Object,Object&gt; startsWithAFilter = new RowFilter&lt;Object,Object&gt;() {
             public boolean include(Entry&lt;? extends Object, ? extends Object&gt; 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);
  </code></pre>
  
  <p>Here is code inside my model that adds value to model:</p>
  
  <pre><code>public void update(Observable o, Object evt) {
      if (evt instanceof ObservableEvent&lt;?&gt;) {
  
          ObservableEvent&lt;?&gt; event = (ObservableEvent&lt;?&gt;) 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 &amp;&amp; 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());
  
                      }
  </code></pre>