public static int calcColumnWidth(JTable table,int col){
  int width=calcHeaderWidth(table,col);
  if (width == -1) {
    return width;
  }
  TableColumnModel columns=table.getColumnModel();
  TableModel data=table.getModel();
  int rowCount=data.getRowCount();
  columns.getColumn(col);
  try {
    for (int row=rowCount - 1; row >= 0; --row) {
      Component c=table.prepareRenderer(table.getCellRenderer(row,col),row,col);
      width=Math.max(width,c.getPreferredSize().width + 10);
    }
  }
 catch (  Exception e) {
    e.printStackTrace();
  }
  return width;
}

public static int calcColumnWidth ( JTable table , int col ) { int width = calcHeaderWidth ( table , col ) ; if ( width == - 1 ) { return width ; } TableColumnModel columns = table . getColumnModel ( ) ; TableModel data = table . getModel ( ) ; int rowCount = data . getRowCount ( ) ; columns . getColumn ( col ) ; try { for ( int row = rowCount - 1 ; row >= 0 ; -- row ) { Component c = table . prepareRenderer ( table . getCellRenderer ( row , col ) , row , col ) ; width = Math . max ( width , c . getPreferredSize ( ) . width + 10 ) ; } } catch ( Exception e ) { e . printStackTrace ( ) ; } return width ; }
calculates the optimal width for the column of the given table . the calculation is based on the preferred width of the header and cell renderer . < br > taken from the newsgoup de . comp . lang . java with some modifications . < br > taken from fopps / enhancedtable - http : / / fopps . sourceforge . net / < br >
