Here you can find the source of layoutHeaders(JTable p_Table)
public static void layoutHeaders(JTable p_Table)
//package com.java2s; //License from project: Open Source License import java.awt.Component; import javax.swing.JTable; import javax.swing.table.TableColumn; public class Main { public static void layoutHeaders(JTable p_Table) { int column = 0; for (java.util.Enumeration columns = p_Table.getTableHeader().getColumnModel().getColumns(); columns .hasMoreElements(); column++) { TableColumn c = (TableColumn) columns.nextElement(); Component component = c.getHeaderRenderer().getTableCellRendererComponent(p_Table, c.getHeaderValue(), false, false, -1, column); c.setPreferredWidth(Math.max(c.getPreferredWidth(), component.getPreferredSize().width)); }// w w w. j av a 2 s.com } }