1

I have a JTable with some data and some empty rows. The table is constructed with a table model with 5 rows of data. But I have it showing and additional 6 empty rows below the 5 filled rows by overloading the getRowCount() to return 11 in the table model.

However when I do a sort, the empty rows will appear at the top of the table. I would like the empty rows to always show at the bottom of the table.

What is the best way of doing this without creating a second table with empty rows tacked on below the one that is sort-able?

Thanks!

TT.
  • 15,774
  • 6
  • 47
  • 88
user1144251
  • 327
  • 1
  • 3
  • 12
  • I found the solution via: http://stackoverflow.com/questions/2316563/how-can-i-sort-java-jtable-with-an-empty-row-and-force-the-empty-row-always-be-last Thanks! – user1144251 Dec 03 '16 at 21:53

2 Answers2

0

Create a TableRowSorter that does the sorting you need, and register it as the sorter using JTable.setRowSorter.

This will involve creating a Comparator that always has the empty rows be greater than non-empty rows based on some or all fields. This will have to take the SortOrder into account if you want to have the empty rows appear at the bottom even if you reverse the sort order. You can get the ordering by RowSorter.getSortKeys.

TT.
  • 15,774
  • 6
  • 47
  • 88
0
The call to getSortKeys().get() must use 0 (i.e. the main sort key) and not column:

boolean ascending = getSortKeys().get(0).getSortOrder() == SortOrder.ASCENDING;
If the user clicks, for example, over the column number 12 or 7, you want the first sort key (there will be 3 at most, if the user clicked before over other columns, to perform multi column sort). This sort key will have two attributes:

Sort Order (ASCENDING, DESCENDING, UNORDERED)
getSortKeys().get(0).getSortOrder()
Column Index (12 or 7, for example)
getSortKeys().get(0).getColumn()