Search code examples
javaswingswingxjxtabletablerowsorter

JXTable - how to update highlighters on sorting the table


I have a JXTable with custom table model. I added 2 ColorHighlighter's with custom HighlightPredicate's.

Problem is when i click on the column header, the table sorts the rows, BUT the highlighter's remain as for the old view.

How can I update the state of the highlight after sorting a table?


Solution

  • As @kleopatra mentioned, i looked at my predicate:

        HighlightPredicate spakowany = new HighlightPredicate() {
            @Override
            public boolean isHighlighted(Component renderer, ComponentAdapter adapter) {
                TableModel tableModel = table.getModel();
                if (tableModel instanceof StanTableModel) {
                    StanTableModel stanTableModel = (StanTableModel) tableModel;
                    // int rowIndex = adapter.row;  <-  this was the issue 
                    int rowIndex = adapter.convertRowIndexToModel(adapter.row);
                    StanTableRow myCustomRow = stanTableModel.getRow(rowIndex);
                    if ((myCustomRow.isSpakowany()) {
                        return true;
                    }
                }
                return false;
            }
        };
    

    and used @mKorbel idea:

    was:

        int rowIndex = adapter.row;
    

    is now:

        int rowIndex = adapter.convertRowIndexToModel(adapter.row);
    

    And it works now.

    StanTableModel is my custom table model. It has getRow() function and returns a StanTableRow object which in turn has isSpakowany() function.