tags:

views:

112

answers:

1

Hi All,

I searched and couldn't find an answer for this. By default column headers in a JTable are centered. How do I make certain column headers left or right justified instead?

TIA

+5  A: 
TableCellRenderer renderer = new DefaultTableCellRenderer();
renderer.setHorizontalAlignment(SwingConstants.RIGHT);
table.getColumn(id).setHeaderRenderer(renderer);

Or, if you don't have the column names available:

table.getColumnModel().getColumn(index).setHeaderRenderer(renderer);
Michael Borgwardt
I get an error because I don't seem to have an ID.I usetable.getColumn(0).setHeaderRenderer(renderer);for column 1and table.getColumn(1).setHeaderRenderer(renderer);for column 2
Dan Howard
this works: table.getColumn(table.getColumnName(1)).setHeaderRenderer(renderer);thanks
Dan Howard