Auto sorting JTable by second column
Hello all
I am building a program that displays files and folders from a specific folder on my drive using a JTable component. I have created a custom renderer to show jlabel components with images and it's all working great. I also created a custom comparator class that does exactly what i want when i press on the table header. The issue is that i cannot find how to simulate a user clicking on the second column. It always defaults to the first one and i've been searching for days for a solution. Here's one i found on the oracle page and even that doesn't work :
TableRowSorter sorter = new TableRowSorter(getModel());
Comparator comp = new FolderTableSorter(false);
sorter.setComparator(0, comp);
sorter.setComparator(1, comp);
sorter.setComparator(2, comp);
List<RowSorter.SortKey> sortKeys = new ArrayList<RowSorter.SortKey>();
sortKeys.add(new RowSorter.SortKey(1, SortOrder.DESCENDING));
sorter.setSortKeys(sortKeys);
sorter.toggleSortOrder(1);
setRowSorter(sorter);
((FolderTableModel)getModel()).fireTableDataChange d();
revalidate();
As you can see i tried all sorts of methods that i thought MIGHT update the table lol. The odd thing is calling togglesortorder actually puts a little triangle in the column header but it doesn't update the table. By the way, this is all hapenning in a JTable subclass with internal nested classes for the renderer and what not.
Any help would be appreciated!
Martin