按列排序时调用的JTable方法

时间:2011-05-30 07:56:43

标签: java swing sorting jtable

当用户单击列标题对相对列进行排序时,是否在JTable(或相关类)中调用了任何方法? 我想在列标题上发生用户点击之前执行一些操作,然后才开始排序操作。那可能吗?

2 个答案:

答案 0 :(得分:2)

javax.swing.RowFilter

import java.awt.*;
import java.awt.event.ActionEvent;
import javax.swing.*;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.event.*;
import javax.swing.table.*;

public class TestTableRowTable {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                try {
                    // UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                        if (info.getName().equals("Nimbus")) {
                            UIManager.setLookAndFeel(info.getClassName());
                            break;
                        }
                    }
                } catch (Exception e1) {
                    e1.printStackTrace();
                }

                final JTable table = new JTable(10, 6);
                for (int i = 0; i < table.getRowCount(); i++) {
                    table.setValueAt(i, i, 0);
                }
                final TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(table.getModel());
                table.setRowSorter(sorter);
                final AbstractTableModel model = new AbstractTableModel() {

                    private static final long serialVersionUID = 1L;

                    @Override
                    public int getColumnCount() {
                        return 1;
                    }

                    @Override
                    public Object getValueAt(int row, int column) {
                        return table.convertRowIndexToModel(row);
                    }

                    @Override
                    public int getRowCount() {
                        return table.getRowCount();
                    }
                };
                JTable headerTable = new JTable(model);
                headerTable.setShowGrid(false);
                headerTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
                headerTable.setPreferredScrollableViewportSize(new Dimension(30, 0));
                headerTable.getColumnModel().getColumn(0).setPreferredWidth(30);
                headerTable.getColumnModel().getColumn(0).setCellRenderer(new TableCellRenderer() {

                    @Override
                    public Component getTableCellRendererComponent(JTable x, Object value, boolean isSelected, boolean hasFocus, int row, int column) {

                        boolean selected = table.getSelectionModel().isSelectedIndex(row);
                        Component component = table.getTableHeader().getDefaultRenderer().getTableCellRendererComponent(table, value, false, false, -1, -2);
                        ((JLabel) component).setHorizontalAlignment(JLabel.CENTER);
                        if (selected) {
                            component.setFont(component.getFont().deriveFont(Font.BOLD));
                        } else {
                            component.setFont(component.getFont().deriveFont(Font.PLAIN));
                        }
                        return component;
                    }
                });
                table.getRowSorter().addRowSorterListener(
                        new RowSorterListener() {

                            @Override
                            public void sorterChanged(RowSorterEvent e) {
                                model.fireTableDataChanged();
                            }
                        });
                table.getSelectionModel().addListSelectionListener(
                        new ListSelectionListener() {

                            @Override
                            public void valueChanged(ListSelectionEvent e) {
                                model.fireTableRowsUpdated(0, model.getRowCount() - 1);
                            }
                        });
                JScrollPane pane = new JScrollPane(table);
                pane.setRowHeaderView(headerTable);
                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                frame.getContentPane().add(pane);
                frame.getContentPane().add(new JButton(new AbstractAction("Toggle filter") {

                    private static final long serialVersionUID = 1L;
                    private RowFilter<TableModel, Object> filter = new RowFilter<TableModel, Object>() {

                        @Override
                        public boolean include(javax.swing.RowFilter.Entry<? extends TableModel, ? extends Object> entry) {
                            return ((Number) entry.getValue(0)).intValue() % 2 == 0;
                            //return ((String) entry.getValue(0)).length()>0 ;
                            //return ((Date) entry.getValue(0)).getTime()< ;
                        }
                    };

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        if (sorter.getRowFilter() != null) {
                            sorter.setRowFilter(null);
                        } else {
                            sorter.setRowFilter(filter);
                        }
                    }
                }), BorderLayout.PAGE_END);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    private TestTableRowTable() {
    }
}

答案 1 :(得分:0)

在Java5中,您可以继承BasicTableHeaderUI并覆盖createMouseInputListener(),为了这个目的,它被声明为受保护的,以返回自定义侦听器

在Java6中,很多摇摆已经改变,不知道它是如何在那里完成的。