Here you can find the source of packColumn(JTable table, int colIndex)
private static void packColumn(JTable table, int colIndex)
//package com.java2s; /*/*from ww w .j ava 2s .c o m*/ RDS Surveyor -- RDS decoder, analyzer and monitor tool and library. For more information see http://www.jacquet80.eu/ http://rds-surveyor.sourceforge.net/ Copyright (c) 2009, 2010 Christophe Jacquet This file is part of RDS Surveyor. RDS Surveyor is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. RDS Surveyor is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser Public License for more details. You should have received a copy of the GNU Lesser Public License along with RDS Surveyor. If not, see <http://www.gnu.org/licenses/>. */ import java.awt.Component; import javax.swing.JTable; import javax.swing.table.TableCellRenderer; import javax.swing.table.TableColumn; import javax.swing.table.TableColumnModel; public class Main { private static void packColumn(JTable table, int colIndex) { TableColumnModel colModel = table.getColumnModel(); TableColumn col = colModel.getColumn(colIndex); int width = 0; // Get width of column header TableCellRenderer renderer = col.getHeaderRenderer(); if (renderer == null) { renderer = table.getTableHeader().getDefaultRenderer(); } Component comp = renderer.getTableCellRendererComponent(table, col.getHeaderValue(), false, false, 0, 0); width = comp.getPreferredSize().width; // Get maximum width of column data for (int r = 0; r < table.getRowCount(); r++) { renderer = table.getCellRenderer(r, colIndex); comp = renderer.getTableCellRendererComponent(table, table.getValueAt(r, colIndex), false, false, r, colIndex); width = Math.max(width, comp.getPreferredSize().width); } // Add margin width += 2 * 8; // Set the width col.setPreferredWidth(width); } }