Sort JTable records without clicking on column header

I implemented row sorting in JTable using DefaultRowSorter and my own row sorter class. Everything works fine by clicking on the column heading to sort the table.

But what about if I want to call a sort operation from my application code (without clicking on the column heading). What method do I need to call?

EDIT:

I initialize the table row sorter as follows:

public void buildRowSorter() { TableRowSorter<MyModel> sorter = new TableRowSorter<MyModel>((MyModel)this.table.getModel()); try { sorter.setComparator(0, new MyCustomComparator<Double>(sorter,0)); sorter.setComparator(1, new MyCustomComparator<String>(sorter,1)); } catch (ParseException e) { e.printStackTrace(); } this.table.setRowSorter(sorter); } 

Now I would like, having a link to JTable (table), to get the associated row sorter into a specific column of my model and invoke a sort operation on it.

+3
source share
2 answers

as far as I can see, you have a custom comparator (why does it keep a link to the sorter? looks suspicious), and not a custom RowSorter.

The supposed way to change the sort is to call toggleSortOrder (column) on the RowSorter. For smaller-scale management, you may need access to DefaultRowSorter, fi its setSortKeys method.

+4
source

Try calling the sort() DefaultRowSorter .

+2
source

Source: https://habr.com/ru/post/1400609/


All Articles