Search code examples
javaswingfilterjtabletablerowsorter

Removing JTable row when sorter is applied causes IndexOutOfBoundsException


I have a JTable with rows of custom objects, one column of which is a time component that counts down. When the countdown reaches 0 the row is to remove itself automatically.

I also have a filter option via textbox that the user can type into to filter the rows by their data.

Without the sorter applied to the JTable, everything works correctly (rows removing themselves when the time counts to 0) besides filtering. Applying the sorter gives me an "java.lang.IndexOutOfBoundsException: Invalid index"

My custom table model looks like this

public Object getValueAt(int rowIndex, int columnIndex) {

    CustomObject myObject = customObjects.get(rowIndex);
    if (columnIndex == MyFirstColumn) {
        return myObject.getData1();
    } else if (columnIndex == MySecondColumn) {
        return myObject.getData2();
    } else if (columnIndex == TimeRemainingColumn) {
        if (myObject.getDate() - System.currentTimeMillis() <= 0) {

            //If I comment this out I can filter but not remove rows
            removeRow(myObject); 

            return 0;
        }
        else{
            fireTableDataChanged();
            return myObject.getDate() - System.currentTimeMillis();
        }  
    }

    return DateFormat.getDateInstance().format(new Date(myObject.getDate()));
}


public void removeRow(CustomObject object) {
    int row = customObjects.indexOf(object);
    customObjects.remove(object);
    fireTableRowsDeleted(row, row);
}

This throws this exception

Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Invalid index
at javax.swing.DefaultRowSorter.convertRowIndexToModel(DefaultRowSorter.java:514)
at javax.swing.JTable.convertRowIndexToModel(JTable.java:2645)
at javax.swing.JTable.getValueAt(JTable.java:2720)
at javax.swing.JTable.prepareRenderer(JTable.java:5718)
at javax.swing.plaf.synth.SynthTableUI.paintCell(SynthTableUI.java:684)
at javax.swing.plaf.synth.SynthTableUI.paintCells(SynthTableUI.java:581)
at javax.swing.plaf.synth.SynthTableUI.paint(SynthTableUI.java:365)
at javax.swing.plaf.synth.SynthTableUI.update(SynthTableUI.java:276)
at javax.swing.JComponent.paintComponent(JComponent.java:778)
at javax.swing.JComponent.paint(JComponent.java:1054)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5221)
....

Going to DefaultRowSorter.java it is hitting this exception

public int convertRowIndexToModel(int index) {
    if (viewToModel == null) {
        if (index < 0 || index >= getModelWrapper().getRowCount()) {
            throw new IndexOutOfBoundsException("Invalid index");
        }
        return index;
    }
    return viewToModel[index].modelIndex;
}

Up Another level, why removing works without a sorter (sorter == null)

public int convertRowIndexToModel(int viewRowIndex) {
    RowSorter sorter = getRowSorter();
    if (sorter != null) {
        return sorter.convertRowIndexToModel(viewRowIndex);
    }
    return viewRowIndex;
}

Debugging a bit firstRow and endRow are both 0 at that point (they should be?) But it seems to be attempting to sort/filter an empty list

Heres my table code

    myModel = new CustomObjectDataModel();

    myTable.setModel(myModel);
    mySorter = new TableRowSorter(myModel);

    myTable.setRowSorter(mySorter);
    myTable.setAutoCreateRowSorter(true);

I've searched around and found dead links to suggesting this is a bug in Java but can't confirm.


Solution

  • I implemented this using Swing Timers instead as suggested and fixed the issue, but the problem here was modifying the table model outside of the EDT.