以编程方式对 JTable 进行排序 [英] Sorting JTable programmatically

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

问题描述

有没有办法以编程方式对 JTable 进行排序?

Is there a way to sort a JTable programmatically?

我的 JTable 排序工作(使用 setRowSorter),以便当用户按下任何列时,表格得到排序.

I have my JTable's sort working (with setRowSorter) so that when the user presses any of the columns, the table gets sorted.

我知道,SWingX JXTable 可能会工作,但我不想经历这些麻烦,因为现在其他一切都可以正常工作,而且我不知道 NetBeans 的可视化编辑器如何处理 JX​​Table 等.

I know, SWingX JXTable would probably work, but I'd rather not go through the hassle because everything else is pretty much working now and I don't know how well NetBeans' visual editor handles JXTable etc.

选定的答案是指我(现已删除)的声明,即 Sun 页面上的答案对我不起作用.那只是我无知造成的环境问题.

推荐答案

对我来说效果很好:

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;

public class TableBasic extends JPanel
{
    public TableBasic()
    {
        String[] columnNames = {"Date", "String", "Integer", "Boolean"};
        Object[][] data =
        {
            {new Date(), "A", Integer.valueOf(1), Boolean.TRUE },
            {new Date(), "B", Integer.valueOf(2), Boolean.FALSE},
            {new Date(), "C", Integer.valueOf(19), Boolean.TRUE },
            {new Date(), "D", Integer.valueOf(4), Boolean.FALSE}
        };

        DefaultTableModel model = new DefaultTableModel(data, columnNames)
        {
            //  Returning the Class of each column will allow different
            //  renderers and editors to be used based on Class

            public Class getColumnClass(int column)
            {
                switch (column)
                {
                    case 0: return Date.class;
                    case 2: return Integer.class;
                    case 3: return Boolean.class;
                }

                return super.getColumnClass(column);
            }
        };


        JTable table = new JTable(model);
        table.setPreferredScrollableViewportSize(table.getPreferredSize());
        table.setAutoCreateRowSorter(true);

        // DefaultRowSorter has the sort() method

        ArrayList<RowSorter.SortKey> list = new ArrayList<>();
        DefaultRowSorter sorter = ((DefaultRowSorter)table.getRowSorter());
        sorter.setSortsOnUpdates(true);
        list.add( new RowSorter.SortKey(2, SortOrder.ASCENDING) );
        sorter.setSortKeys(list);
        sorter.sort();

        JScrollPane scrollPane = new JScrollPane( table );
        add( scrollPane );
    }

    private static void createAndShowGUI()
    {
        JFrame frame = new JFrame("Table Basic");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new TableBasic());
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationByPlatform( true );
        frame.setVisible( true );
    }

    public static void main(String[] args) throws Exception
    {
        SwingUtilities.invokeLater( () -> createAndShowGUI() );
    }
}

下次在出现问题时发布您的最小的、可重现的示例.

Next time post your minimal, reproducible example when something doesn't work.

这篇关于以编程方式对 JTable 进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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