tags:

views:

273

answers:

1

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
    }
});
+2  A: 

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?

Michael Myers
Perfect. Thanks a bunch mmyers!
Chris Cowdery-Corvan