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

public static int calcColumnWidth ( JTable table , int col ) { int result ; TableModel data ; int rowCount ; int row ; int dec ; Component c ; result = calcHeaderWidth ( table , col ) ; if ( result == - 1 ) return result ; data = table . getModel ( ) ; rowCount = data . getRowCount ( ) ; dec = ( int ) Math . ceil ( ( double ) rowCount / ( double ) MAX_ROWS ) ; try { for ( row = rowCount - 1 ; row >= 0 ; row -= dec ) { c = table . prepareRenderer ( table . getCellRenderer ( row , col ) , row , col ) ; result = Math . max ( result , c . getPreferredSize ( ) . width + 10 ) ; } } catch ( Exception e ) { e . printStackTrace ( ) ; } return result ; }
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 newsgroup de . comp . lang . java with some modifications . < br > taken from fopps / enhancedtable - http : / / fopps . sourceforge . net / < br >
