Multisort Table Header Cell Renderer
Posted by Darryl Burke on August 29, 2010
It’s easy to make a JTable sortable by invoking setAutoCreateRowSorter(true). Clicking on the header of a column will then sort the rows on the basis of the contents of the cell in that column, and display an appropriate arrow icon. A little known fact is that the default, automatically created, RowSorter actually uses the last three sort indexes. Unfortunately, the only way the user can know which columns are involved in the subordinate sorting is by remembering the sequence in which the columns were clicked.
MultisortTableHeaderCellRenderer addresses this deficiency by showing the sort icons with decreasing opacity on all columns involved in the sort, making use of the AlphaIcon class published earlier.
The default alpha of 0.5 has been chosen as suitable for the default maximum of three sort keys. With this value, however, the fourth sort icon is barely visible and the fifth all but invisible. If you are using a custom RowSorter that caters to a maximum of more than three sort keys, you may like to use a MultisortTableHeaderCellRenderer with a higher value of alpha. An alpha of 0.7 is about the limit, and allows up to seven or eight sort icons to be visible, as shown in the illustration below. Any higher value results in inadequate contrast between the primary and secondary sort icons.
MultisortTableHeaderCellRenderer is used in much the same way as its superclass, with the difference that a float value may be passed to its constructor to obtain a non-default opacity.
MultisortTableHeaderCellRenderer extends DefaultTableHeaderCellRenderer