简体   繁体   中英

Initial state of autoCreateRowSorter in Swing JTable

I have this JTable on my Swing app with the autoCreateRowSorter enabled. My table only has 3 columns, two strings and one int, it works well for all of them when I click the column headers.

However, I'm looking for way to do it programatically. I wanted to set the "initial state" for this table. With the Windows look and feel, the column header (when sorted) has a little arrow showing the sort order. But at startup that doesn't show, I have to do one initial click.

How can I do that by code?

To programaticallly sort the table you can do something like:

DefaultRowSorter sorter = ((DefaultRowSorter)table.getRowSorter());
ArrayList list = new ArrayList();
list.add( new RowSorter.SortKey(0, SortOrder.ASCENDING) );
sorter.setSortKeys(list);
sorter.sort();

我认为DefaultRowSorter#toggleSortOrder(int column)将完成工作

You don't even need the DefaultRowSorter interface.

table.getRowSorter().toggleSortOrder(column);

works as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM