javaswingjtabletablerowsorterrowsorter

Get column which fires RowSorterEvent


I have a RowSorterListener. I would like to be able to tell which column fires the RowSorterEvent. However when I attempt to get the column, I do not get the output I desire.

public class CustomRowSorterListener implements RowSorterListener {
    JTable table;
    public CustomRowSorterListener(JTable table) {
        this.table = table;
    }

    @Override
    public void sorterChanged(RowSorterEvent e)
    {
        //Attempt 1
        System.out.println(e.getSource()); //Returns RowSorter and not a column

        //Attempt 2
        System.out.println(e.getSource().getColumn()); //Caused error in code

        //Attempt 3
        System.out.println(table.getColumn()) //Didn't work because no arguments were 
                                              //provided. However, I was unsure if it could
                                              //be done this way.

        //System.out.println(table.getColumn(e.getSource())) also doesn't work
    }
}

I would appreciate any help in getting a solution which works.


Solution

  • I would like to be able to tell which column fires the RowSorterEvent.

    Start with the SortKey. It can return getColumn. For example:

    Column NO. - 0 is sorted
    Column NO. - 0 is sorted
    Column NO. - 0 is sorted
    Column NO. - 1 is sorted
    Column NO. - 1 is sorted
    BUILD SUCCESSFUL (total time: 9 seconds)

    import java.awt.EventQueue;
    import java.util.List;
    
    import javax.swing.*;
    import javax.swing.RowSorter.SortKey;
    import javax.swing.event.*;
    import javax.swing.table.*;
    
    public class SortTest {
    
        private JFrame frame = new JFrame(getClass().getSimpleName());
    
        private DefaultTableModel model = new DefaultTableModel(10, 2) {
            private static final long serialVersionUID = 1L;
    
            @Override
            public Class<?> getColumnClass(int column) {
                return column == 1 ? Integer.class : Object.class;
            }
        };
        private JTable table = new JTable(model);
    
        public SortTest() {
            for (int row = model.getRowCount(); --row >= 0;) {
                int i = 20 + row % 20;
                model.setValueAt(row + " " + i, row, 0);
                model.setValueAt(i + row, row, 1);
            }
            table.setAutoCreateRowSorter(true);
            TableRowSorter<?> sorter = (TableRowSorter<?>) table.getRowSorter();
            sorter.setSortsOnUpdates(true);
            sorter.addRowSorterListener(new RowSorterListener() {
    
                @Override
                public void sorterChanged(RowSorterEvent rse) {
                    if (rse.getType() == RowSorterEvent.Type.SORT_ORDER_CHANGED) {
                        List<SortKey> keys = rse.getSource().getSortKeys();
                        for (SortKey key : keys) {
                            System.out.println("Column NO. - " + key.getColumn() + " is sorted");
                            if (key.getColumn() == 0) {
                                break;
                            } else {
                                break;
                            }
                        }
                    }
                }
            });
            frame.add(new JScrollPane(table));
            table.setPreferredScrollableViewportSize(table.getPreferredSize());
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            EventQueue.invokeLater(() -> {
                new SortTest();
            });
        }
    }