final JTable jtable = new JTable( new MyTableModel() );
final TableColumnModel tableColumnModel = jtable.getColumnModel();
final TableCellRenderer headerRenderer = new HeaderRenderer();
for ( int i = 0; i < cols; i++ )
{
tableColumnModel.getColumn( i ).setHeaderRenderer( headerRenderer );
}
/**
* nested inner class to render a column heading
*/
static class HeaderRenderer implements TableCellRenderer {
public Component getTableCellRendererComponent( JTable table,
Object value,
boolean isSelected,
boolean hasFocus,
int row,
int column )
{
headerTemplate.setText( value.toString() );
return headerTemplate;
}
/**
* template for HeaderRender, reuses same component for all headers.
*/
private static final JLabel headerTemplate;
static
{
headerTemplate = new JLabel("",JLabel.CENTER);
headerTemplate.setForeground( LABEL_COLOUR );
headerTemplate.setBackground( BACKGROUND );
headerTemplate.setBorder( BorderFactory.createLineBorder( Color.GRAY ) );
headerTemplate.setOpaque( true );
}
}