java 以编程方式对 JTable 进行排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1539329/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 17:00:26  来源:igfitidea点击:

Sorting JTable programmatically

javaswing

提问by auramo

Is there a way to sort a JTable programmatically?

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

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

我让 JTable 的排序工作(使用 setRowSorter),这样当用户按下任何列时,表格就会被排序。

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.

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

EDIT:The selected answer is referring to my (now removed) statement that the answer from Sun's pages didn't work for me. That was just an environment issue caused by my ignorance.

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

回答by camickr

Works fine for me:

对我来说很好用:

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

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

        final JTable table = new JTable(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)
            {
                // Lookup first non-null data on column
                for (int row = 0; row < getRowCount(); row++) 
                {
                    Object o = getValueAt(row, column);

                    if (o != null)
                        return o.getClass();
                }

                return Object.class;
            }
        };
        table.setPreferredScrollableViewportSize(table.getPreferredSize());
        table.setAutoCreateRowSorter(true);
        // DefaultRowSorter has the sort() method
        DefaultRowSorter sorter = ((DefaultRowSorter)table.getRowSorter()); 
        ArrayList list = new ArrayList();
        list.add( new RowSorter.SortKey(2, SortOrder.ASCENDING) );
        sorter.setSortKeys(list);
        sorter.sort();

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

    public static void main(String[] args)
    {
        TableBasic frame = new TableBasic();
        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible(true);
    }
}

Next time post your SSCCE when something doesn't work.

下次当某些事情不起作用时发布您的 SSCCE。

回答by Matthieu

You also can trigger row sorting by calling toggleSortOrder()on the RowSorterof your JTable:

您也可以通过调用触发行排序toggleSortOrder()RowSorter您的JTable

table.getRowSorter().toggleSortOrder(columnIndex);

Note, however, that (quoting the Javadoc):

但是,请注意(引用Javadoc):

Reverses the sort order of the specified column. Typically this will reverse the sort order from ascending to descending (or descending to ascending) if the specified column is already the primary sorted column.

反转指定列的排序顺序。通常,如果指定的列已经是主要排序列,这会将排序顺序从升序反转为降序(或降序为升序)。

Though it is quicker, calling setSortKeys()as shown by @camickr (+1 to him) is the correct way to go (but you need to instanciate a List).

虽然它更快,但setSortKeys()如@camickr(对他+1)所示的调用是正确的方法(但你需要实例化 a List)。