}
public static void autoSizeTableColumns(JTable table) {
TableModel model = table.getModel();
TableColumn column = null;
Component comp = null;
int headerWidth = 0;
int maxCellWidth = Integer.MIN_VALUE;
int cellWidth = 0;
TableCellRenderer headerRenderer =
table.getTableHeader().getDefaultRenderer();
for (int i = 0; i < table.getColumnCount(); i++) {
column = table.getColumnModel().getColumn(i);
comp = headerRenderer.getTableCellRendererComponent(table,
column.getHeaderValue(), false, false, 0, 0);
headerWidth = comp.getPreferredSize().width + 10;
maxCellWidth = Integer.MIN_VALUE;
for (int j = 0; j < Math.min(model.getRowCount(), 30); j++) {
TableCellRenderer r = table.getCellRenderer(j, i);
comp = r.getTableCellRendererComponent(table,
model.getValueAt(j, i),
false, false, j, i);
cellWidth = comp.getPreferredSize().width;
if (cellWidth >= maxCellWidth) {
maxCellWidth = cellWidth;
}
}
column.setPreferredWidth(Math.max(headerWidth, maxCellWidth)
+ 10);
}
}