3

I am currently trying to wrap and align center text of a column header. The problem seems to be that while the first line of column header is aligned, the second one does not get aligned properly.

I am using DefaultTableCellRenderer to render it as such:

public void centerAlign(JTable t, int numberOfColumns){
    centerRenderer.setHorizontalAlignment(SwingConstants.CENTER);
    for (int i = 0; i < numberOfColumns; i++){
         t.getColumnModel().getColumn(i).setCellRenderer(centerRenderer);
    }
    headerRender = (DefaulttableCellRenderer)
    t.getTableHeader().getDefaultRenderer();
    headerRenderer.setHorizontalAlignment(JLabel.CENTER);
}
KyleKW
  • 300
  • 1
  • 11
  • 1
    you define 'headerRender' but then call it 'headerRenderer' when you set the alignment on the last line. – Reddy Oct 20 '16 at 12:28
  • 1
    For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Oct 20 '16 at 12:36
  • Take a look at [this answer](http://stackoverflow.com/a/7494597/243373) from the mighty TrashGod on the question "JTable Right Align Header". – TT. Oct 20 '16 at 13:11

1 Answers1

3

In your table model class use html of column name

example:

"<html><center>First column</html>"
Piotr Rogowski
  • 3,642
  • 19
  • 24