获取触发 RowSorterEvent 的列

Get column which fires RowSorterEvent

我有一个 RowSorterListener。我想知道哪个列触发了 RowSorterEvent。但是,当我尝试获取该列时,我没有得到我想要的输出。

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 like to be able to tell which column fires the RowSorterEvent.

SortKey 开始。它可以 return getColumn。例如:

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();
        });
    }
}