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:
table.getTableHeader().
setDefaultRenderer(
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
DefaultTableHeaderCellRenderer.java
See Also
Vertical Table Header Cell Renderer
Milan said
Thanks alot, this was REALLY helpful!!!
Darryl Burke said
Thank you, Milan.
db
Ken said
Darryl, I agree with Milan- thanks!
Darryl Burke said
Thank you, Ken
db
Jorgen said
Please confirm that it is free to use and rip out for any purpose?
Thanks.
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.
db
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.
Darryl Burke said
Thank you for noticing that, and for providing a fix. The code has been updated.
Darryl
SteveH said
Thanks. I needed a custom header renderer, but lost the sort icon. This worked perfectly!
GrassEh said
Like 3 years later, this is still helpful. Thank Darryl ! Link to this is in my code.
Anonymous said
Very useful. Solved my problem with rendering of header and different alignment issues
f1ben said
Thanks, Darryl. This was helpful. Looks like you forgot the breaks in your switch statement in the getIcon method.
Darryl Burke said
No, I didn’t.
Try adding break statements and see whether that compiles.
Darryl
f1ben said
Ah, my mistake. I changed the method to avoid the early returns before I got a warning related to the missing breaks. Never mind.
Wesley Ding said
At last I noticed that this custom class is `DefaultTableHeaderCellRenderer`, not `sun.swing.table.DefaultTableCellHeaderRenderer` in the API. So annoying… but thanks.