简体   繁体   中英

Ordering Jtable items using a particular column - JAVA

Trying to order a jtable by the Days of week column since the day of week names are not in order once they are displayed on the jtable. Beans binding has been used to link database(MYSQL) with the jtable, but i need an event to sort the entries by the day of week column (Mon, Tues....in this order).

There are out there different kind of sortable JTables and you might have a comparator for each columns if you want. Samples can be found at java2s and also you might consider SwingX 's table where as well you can specify your own comparator.

You can add a RowSorter to the JTable and the user can click on any heading to do a sort.

If you want to presort the data then you can manually do a sort:

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

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