• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Centering in JTable

 
Matt DeLacey
Ranch Hand
Posts: 318
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does anyone know to center data in a JTable? I'm using a TableModel but can't seem to figure out how to do this. I know if you add an ImageIcon to a table it is supposedly centered. Should you just try to trick the table into thinking that your string is an ImageIcon?! That seems weird and messy. Surely, there is some simple way, I just can't find it. Also, I'd LIKE to make it so the user can't modify the widths of the columns but haven't figured this one yet either.
With Respect,
Matt DeLacey
 
alex zhanov
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Matt.
Look at getColumnClass method of the AbstractTableModel. Based on the return type - JTable will choose the appropriate renderer. Or a more long winded approach, try to register your own column renderer, look at getTableCellRendererComponent method of TableCellRenderer. Plus, I have seen recommendation to look at Sun's Look and Feel specs and see whether the data type you are trying to center align should be there or maybe left-justified.
Alex
 
Matt DeLacey
Ranch Hand
Posts: 318
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Alex,
Many thanks.
With Respect,
Matt
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic