JTable Sorting based on hidden column

后端 未结 3 1442
感情败类
感情败类 2021-01-22 06:17

I would like to sort JTable rows based on one hidden column.

Say I have a JTable like this

column1   column2
val1       val2
相关标签:
3条回答
  • 2021-01-22 07:06

    One approach is to implement the Comparable interface for the type Column2, as shown in this example, and compare using the corresponding Column3 value. Conveniently, if Column3 already implements Comparable<Column3>, you can just delegate to Column3, as the example does with Double.

    0 讨论(0)
  • 2021-01-22 07:08

    you can add by default TableRowSorter to JTable but there is RowSorter, nothing better and clear around as Darryl's Multisort Table Header Cell Renderer

    note definitions for RowSorter is valid only for concrete TableColumn

    siple example (with use-less balast again)

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.table.*;
    
    public class HeaderDoubleclickTest {
    
        private String[] columnNames = {"String", "Integer", "Boolean"};
        private Object[][] data = {
            {"aaa", 12, true}, {"bbb", 5, false},
            {"CCC", 92, true}, {"DDD", 0, false}
        };
        private TableModel model = new DefaultTableModel(data, columnNames) {
    
            private static final long serialVersionUID = 1L;
    
            @Override
            public Class<?> getColumnClass(int column) {
                return getValueAt(0, column).getClass();
            }
        };
        private JTable table = new JTable(model);
        private JTableHeader header;
    
        static class TestTableRowSorter extends TableRowSorter<TableModel> {
    
            TestTableRowSorter(TableModel m) {
                super(m);
            }
    
            @Override
            public void toggleSortOrder(int column) {
            }
    
            public void wrapToggleSortOrder(int column) {
                super.toggleSortOrder(column);
            }
        }
        private Timer timer = new Timer(400, new ActionListener() {
    
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("single");
                JTable table = header.getTable();
                RowSorter sorter;
                if (pt != null && table != null && (sorter = table.getRowSorter()) != null) {
                    int columnIndex = header.columnAtPoint(pt);
                    if (columnIndex != -1) {
                        columnIndex = table.convertColumnIndexToModel(columnIndex);
                        ((TestTableRowSorter) sorter).wrapToggleSortOrder(columnIndex);
                    }
                }
            }
        });
        private Point pt;
    
        public JComponent makeUI() {
            timer.setRepeats(false);
            table.setRowSorter(new TestTableRowSorter(model));
            header = table.getTableHeader();
            header.addMouseListener(new MouseAdapter() {
    
                @Override
                public void mouseClicked(final MouseEvent e) {
                    if (timer.isRunning() && !e.isConsumed() && e.getClickCount() > 1) {
                        System.out.println("double");
                        pt = null;
                        timer.stop();
                    } else {
                        pt = e.getPoint();
                        timer.restart();
                    }
                }
            });
            JPanel p = new JPanel(new BorderLayout());
            p.add(new JScrollPane(table));
            return p;
        }
    
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    
        public static void createAndShowGUI() {
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            f.getContentPane().add(new HeaderDoubleclickTest().makeUI());
            f.setSize(320, 240);
            f.setLocationRelativeTo(null);
            f.setVisible(true);
        }
    }
    
    0 讨论(0)
  • 2021-01-22 07:18

    Use code posted here: http://www.esus.com/docs/GetQuestionPage.jsp?uid=1270

    there's variable / method argument col - just check if its yours column2 and if so treat it like it was your hidden column - sort by it and rerender table.

    0 讨论(0)
提交回复
热议问题