Here you can find the source of adjustHeader(JTable table)
public static void adjustHeader(JTable table)
//package com.java2s; //License from project: Apache License import java.awt.Component; import javax.swing.JTable; import javax.swing.table.TableCellRenderer; import javax.swing.table.TableColumn; public class Main { public static void adjustHeader(JTable table) { TableCellRenderer headerRenderer = table.getTableHeader().getDefaultRenderer(); for (int i = 0; i < table.getColumnCount(); i++) { TableColumn column = table.getColumnModel().getColumn(i); Component header = headerRenderer.getTableCellRendererComponent(table, column.getHeaderValue(), false, false, 0, i);/* ww w .j av a 2 s . c om*/ column.setPreferredWidth(header.getPreferredSize().width + 8); } } }