Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>In your table model:</p> <pre><code>public void delte_raw(int raw) { if (!ls.isEmpty()) { this.fireTableRowsDeleted(raw+1, raw); // why raw+1 ??? ls.remove(raw); } } </code></pre> <p>As your table model extends from <code>AbstractTableModel</code> and looking at <a href="http://docs.oracle.com/javase/7/docs/api/javax/swing/table/AbstractTableModel.html#fireTableRowsDeleted%28int,%20int%29" rel="nofollow">fireTableRowsDeleted(int firstRow, int lastRow)</a> javadoc:</p> <blockquote> <p>Notifies all listeners that rows in the range <code>[firstRow, lastRow]</code>, inclusive, have been deleted.</p> </blockquote> <p>So it should be:</p> <pre><code>public void delte_raw(int raw) { if (!ls.isEmpty()) { ls.remove(raw); // remove the row index from the List and then fire the event fireTableRowsDeleted(raw, raw); } } </code></pre> <p><strong>Knowing the exception source:</strong> looking at <code>DefaultRowSorter.checkAgainstModel(int firstRow, int endRow)</code> implementation:</p> <pre><code>private void checkAgainstModel(int firstRow, int endRow) { if (firstRow &gt; endRow || firstRow &lt; 0 || endRow &lt; 0 || firstRow &gt; modelRowCount) { throw new IndexOutOfBoundsException("Invalid range"); } } </code></pre> <p>As you can see, calling this method with <code>[raw+1,raw]</code> range causes an <code>IndexOutOfBoundsException</code>.</p> <p><strong>Edit</strong></p> <p>As @mKorbel masterfully points out, I've totally overlooked this:</p> <pre><code>int raw = table.getSelectedRow(); // this is the index in the view model.delte_raw(raw); // convert raw in the right model index is needed </code></pre> <p>You need to convert <code>raw</code> in the right model index. Otherwise it can cause side effects since in a sorted table is most likely the selected index in the view be different than its related model's index:</p> <pre><code>int raw = table.getSelectedRow(); // this is the index in the view model.delte_raw(table.convertRowIndexToModel(raw)); // perfect </code></pre> <p>See <a href="http://docs.oracle.com/javase/7/docs/api/javax/swing/JTable.html#convertRowIndexToModel%28int%29" rel="nofollow">JTable.convertRowIndexToModel(int viewRowIndex)</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload