Java Tips Weblog

  • Blog Stats

    • 2,189,668 hits
  • Categories

  • Archives

  • Advertisements

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.

Multisort Table Header Cell Renderer Demo

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.

      new MultisortTableHeaderCellRenderer());

MultisortTableHeaderCellRenderer extends DefaultTableHeaderCellRenderer

Try The Demo

Launch – Using Java™ Web Start (JRE 6 required)

Get The Code

See Also

Default Table Header Cell Renderer
Alpha Icons

Related Reading

Java API: javax.swing.RowSorter
Java API: javax.swing.DefaultRowSorter
Java API: javax.swing.table.TableRowSorter


4 Responses to “Multisort Table Header Cell Renderer”

  1. Eitan said

    Nice to see you active again…

  2. George said

    I get NullPointerException at MultisortTableHeaderCellRenderer.getIcon(
    here: ” for (RowSorter.SortKey sortKey : table.getRowSorter().getSortKeys()) {”

    Any idea why?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: