Here you can find the source of setColumnMinWidths(final JTable table)
public static void setColumnMinWidths(final JTable table)
//package com.java2s; import javax.swing.JTable; import javax.swing.table.TableCellRenderer; import javax.swing.table.TableColumn; import javax.swing.table.TableColumnModel; public class Main { /**// w w w.j av a 2 s .com * set the widths of all of the columns to the max width of the object in the * column. * * @pre (table != null) **/ public static void setColumnMinWidths(final JTable table) { final TableColumnModel colModel = table.getColumnModel(); /** * @assert (colModel.getColumnCount() == table.getColumnCount()), * "table and column model don't agree"; **/ final int columns = colModel.getColumnCount(); for (int columnIndex = 0; columnIndex < columns; columnIndex++) { int maxWidth = 0; final TableColumn column = colModel.getColumn(columnIndex); /** * @assert (column != null) **/ TableCellRenderer renderer = column.getCellRenderer(); if (renderer == null) { renderer = table.getDefaultRenderer(table.getColumnClass(columnIndex)); } if (renderer != null) { final int rows = table.getRowCount(); for (int rowIndex = 0; rowIndex < rows; rowIndex++) { final Object object = table.getValueAt(rowIndex, columnIndex); maxWidth = Math.max(maxWidth, renderer .getTableCellRendererComponent(table, object, false, false, rowIndex, columnIndex) .getPreferredSize().width); } // ????? // maxWidth = Math.max(maxWidth, column.getWidth()); renderer = column.getHeaderRenderer(); if (renderer == null) { renderer = table.getTableHeader().getDefaultRenderer(); } maxWidth = Math.max(maxWidth, renderer.getTableCellRendererComponent(table, column.getHeaderValue(), false, false, -1, columnIndex).getPreferredSize().width); column.setMinWidth(maxWidth + 5); } } } }