基于隐藏列的JTable排序 [英] JTable Sorting based on hidden column

查看:106
本文介绍了基于隐藏列的JTable排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想基于一个隐藏列对 JTable 行进行排序。

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

假设我有一个类似JTable的行这个

Say I have a JTable like this

column1   column2
val1       val2

现在我还有一个隐藏的栏目3,我不想展示。当用户点击Column2时,它应该基于Column3(隐藏列)而不是基于Column2对行进行排序。

Now I have a one more column3 which is hidden and I dont want to show. When user clicks on Column2 it should sort rows based on Column3 (hidden column) not based Column2.

如何在JTable中实现这一点?

How to achieve this in JTable?

推荐答案

您可以默认添加 TableRowSorter JTable 但是有 RowSorter ,没有什么比Darryl的多重表头标题单元格渲染器

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

注意RowSorter的定义仅对具体的TableColumn有效

note definitions for RowSorter is valid only for concrete TableColumn

siple示例(再次使用无用的balast)

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

这篇关于基于隐藏列的JTable排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆