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
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
posted 19 years ago
Hey Alex, Many thanks. With Respect, Matt
I will suppress my every urge. But not this shameless plug:
Devious Experiments for a Truly Passive Greenhouse!