Monday, February 21, 2011

How to left or right justify a column header in a JTable

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

From stackoverflow
  • 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);
    
    Dan Howard : I get an error because I don't seem to have an ID. I use table.getColumn(0).setHeaderRenderer(renderer); for column 1 and table.getColumn(1).setHeaderRenderer(renderer); for column 2
    Dan Howard : this works: table.getColumn(table.getColumnName(1)).setHeaderRenderer(renderer); thanks

0 comments:

Post a Comment