Here you can find the source of adjustColumnWidth(JTable tbl, int col, int maxColumnWidth)
public static void adjustColumnWidth(JTable tbl, int col, int maxColumnWidth)
//package com.java2s; // License: GPL. For details, see LICENSE file. import java.awt.Component; import javax.swing.JTable; import javax.swing.table.TableCellRenderer; public class Main { /**/*w w w .j a v a 2 s.co m*/ * (originally from @class org.openstreetmap.josm.gui.preferences.SourceEditor) * adjust the preferred width of column col to the maximum preferred width of the cells * requires JTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); */ public static void adjustColumnWidth(JTable tbl, int col, int maxColumnWidth) { int maxwidth = 0; for (int row = 0; row < tbl.getRowCount(); row++) { TableCellRenderer tcr = tbl.getCellRenderer(row, col); Object val = tbl.getValueAt(row, col); Component comp = tcr.getTableCellRendererComponent(tbl, val, false, false, row, col); maxwidth = Math.max(comp.getPreferredSize().width, maxwidth); } tbl.getColumnModel().getColumn(col).setPreferredWidth(Math.min(maxwidth + 10, maxColumnWidth)); } }