I am using the following code:
public final class TableCellRendererCenter extends DefaultTableCellRenderer {
public static final TableCellRenderer INSTANCE
= new TableCellRendererCenter();
protected TableCellRendererCenter() {
// Calling super
super();
this.setHorizontalAlignment(SwingConstants.CENTER);
}
}
on a JTable column:
TableColumnModel retrMod = ChartItemsTable.getColumnModel();
TableColumn retrCol = retrMod.getColumn(2);
retrCol.setHeaderRenderer(TableCellRendererCenter.INSTANCE);
retrCol.setCellRenderer(TableCellRendererCenter.INSTANCE);
and the look-and-feel does not match other column headers anymore:

Why? How can I solve this issue?
EDIT
It seems like NetBeans is using DefaultTableCellHeaderRenderer which is coming from package sun.swing.table;
I read everywhere that I should not use sun packages. Grrrr… this does not help!
EDIT 2
The suggestion of 0verbose produces the following:

SOLUTION
Following eugener’s suggestion, I updated my code as following:
public final class TableCellRendererCenter extends DefaultTableCellRenderer {
@Override
public Component getTableCellRendererComponent(
JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column) {
// returns component used for default header rendering
// makes it independent on current L&F
Component retr = table.getTableHeader().getDefaultRenderer().
getTableCellRendererComponent(
table, value, isSelected, hasFocus, row, column);
if ( JLabel.class.isAssignableFrom(retr.getClass()) ) {
JLabel jl = (JLabel) retr;
jl.setHorizontalAlignment(SwingConstants.CENTER);
}
return retr;
}
@Override
public void validate() {}
@Override
public void revalidate() {}
@Override
public void firePropertyChange(
String propertyName, boolean oldValue, boolean newValue) {}
@Override
public void firePropertyChange(
String propertyName, Object oldValue, Object newValue) {}
}
And I get this:

Look-and-feel is preserved and column header is centered !!! (Other column headers are centered too, but I can control this by making further tests on the int row, int column parameters).
The issue is that you are using different cell renderer then the one used by your LAF. IMO the only way to achieve proper LAF is to use renderer already provided by LAF itself such as:
Obviously, once you have proper renderer, you can cast it to a JLabel (after checking that it is, of course) and then center your text or do something else.