Java Tips Weblog

  • Blog Stats

    • 2,189,652 hits
  • Categories

  • Archives

  • Advertisements

Default Table Header Cell Renderer

Posted by Darryl Burke on February 27, 2009

The javax.swing package and its subpackages provide a fairly comprehensive set of default renderer implementations, suitable for customization via inheritance. A notable omission is the lack of a default renderer for a JTableHeader in the public API. The renderer used by default is a Sun proprietary class, sun.swing.table.DefaultTableCellHeaderRenderer, which cannot be extended.

DefaultTableHeaderCellRenderer seeks to fill this void, by providing a rendering designed to be identical with that of the proprietary class, with one difference: the vertical alignment of the header text has been set to BOTTOM, to provide a better match between DefaultTableHeaderCellRenderer and other custom renderers.

The name of the class has been chosen considering this to be a default renderer for the cells of a table header, and not the table cells of a header as implied by the proprietary class name.

To use the renderer, set it to a JTable’s header:

    new DefaultTableHeaderCellRenderer());

This will not lead to any observable change in the table header’s rendering in the default Metal L&F. The strength of DefaultTableHeaderCellRenderer lies in its ability to be extended and customized. A future posting will provide a subclass to render the table header vertically, making use of the already published VerticalLabelUI.

DefaultTableHeaderCellRenderer extends javax.swing.table.DefaultTableCellRenderer.

Get The Code

See Also

Vertical Table Header Cell Renderer

Related Reading

Java API: javax.swing.table.DefaultTableCellRenderer


16 Responses to “Default Table Header Cell Renderer”

  1. Milan said

    Thanks alot, this was REALLY helpful!!!

  2. Darryl Burke said

    Thank you, Milan.


  3. Ken said

    Darryl, I agree with Milan- thanks!

  4. Jorgen said

    Please confirm that it is free to use and rip out for any purpose?


    • Darryl Burke said

      Sure. A credit in the code comments, with a link to this page, would be nice, but is by no means obligatory.


  5. mbg said

    Hello Darryl,

    thanks for this code, it was indeed very helpful.

    I encountered one problem while using this class. If a table provides row sorting by pressing the column header and you reorder the columns per drag&drop than the sorting icon is wrongly placed.

    Instead of
    setIcon(getIcon(table, column));

    you should use
    setIcon(getIcon(table, table.convertColumnIndexToView(column)));

    • mbg said

      I need to correct myself, because suggested change is not working correctly. This fix should be done in the getIcon Method:

      Instead of:
      if (sortKey != null && sortKey.getColumn() == column) {

      it should be:
      if (sortKey != null && table.convertColumnIndexToView(sortKey.getColumn()) == column) {

      This works for me.

  6. SteveH said

    Thanks. I needed a custom header renderer, but lost the sort icon. This worked perfectly!

  7. GrassEh said

    Like 3 years later, this is still helpful. Thank Darryl ! Link to this is in my code.

  8. Anonymous said

    Very useful. Solved my problem with rendering of header and different alignment issues

  9. f1ben said

    Thanks, Darryl. This was helpful. Looks like you forgot the breaks in your switch statement in the getIcon method.

  10. At last I noticed that this custom class is `DefaultTableHeaderCellRenderer`, not `sun.swing.table.DefaultTableCellHeaderRenderer` in the API. So annoying… but thanks.

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: