Chris Cowdery-Corvan
Chris Cowdery-Corvan

Reputation: 649

Performing an action after a RowSort operation

I have a JTable and a TableRowSorter which I'd like to perform an operation after a sort is finished. I've been browsing the net, and so far I haven't had much luck.

Initially I thought just a RowSorterListener would do the trick, but unfortunately it doesn't perform the operation after the sort is finished.

Adding a MouseListener to the JTable header could work, but the solution isn't terribly elegant.

Does anyone have any ideas?

Thanks a bunch!


Edit (from comment): The following is added in a method inside a custom TableModel class which extends AbstractTableModel. This method is invoked whenever the JTable is set/specified in the custom TableModel class.

sorter.addRowSorterListener(new RowSorterListener() {
    @Override public void sorterChanged(RowSorterEvent rowsorterevent) {
        rebuildMItems(); // The method which executes
    }
});

Upvotes: 3

Views: 2369

Answers (1)

Michael Myers
Michael Myers

Reputation: 191915

Two possibilities:

  1. I see you have a custom RowSorter. Couldn't you simply add a call to your operation at the end of the sort() method?

    In other words, can you add this:

    @Override
    public void sort() {
        super.sort();
        doSomethingAfterSortingIsDone();
    }
    

    to your sorter?

  2. Your current method (doing it in a RowSorterListener) performs the operation twice: once for SORT_ORDER_CHANGED and once for SORTED. Can you check the event's type and only perform the operation at the correct time?

Upvotes: 3

Related Questions