Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to invoke the auto row sorter in a jtable

is there anyway to invoke the auto row sorter in a jtable that is created by using

setAutoCreateRowSorter(true);

i'm trying to get it to sort by a default column without the user having to click on on the column header.

like image 555
user979490 Avatar asked Oct 26 '11 14:10

user979490


People also ask

How to sort RowS in JTable?

We can sort a JTable in a particular column by using the method setAutoCreateRowSorter() and set to true of JTable class.

What is TableRowSorter in java?

An implementation of RowSorter that provides sorting and filtering using a TableModel . The following example shows adding sorting to a JTable : TableModel myModel = createMyTableModel(); JTable table = new JTable(myModel); table. setRowSorter(new TableRowSorter(myModel));


3 Answers

table.getRowSorter().toggleSortOrder(modelColumnIndex)
like image 140
kleopatra Avatar answered Oct 20 '22 17:10

kleopatra


TableRowSorter rowSorter = (TableRowSorter) table.getRowSorter();
List<SortKey> keys = new ArrayList<SortKey>();
SortKey sortKey = new SortKey(2, SortOrder.ASCENDING);//column index 2
keys.add(sortKey);
rowSorter.setSortKeys(keys);
rowSorter.sort();
like image 33
camickr Avatar answered Oct 20 '22 16:10

camickr


i'm trying to get it to sort by a default column without the user having to click on on the column header.

I think you have to use setSortsOnUpdates(true) method from TableRowSorter class.

like image 30
Eng.Fouad Avatar answered Oct 20 '22 16:10

Eng.Fouad