private void adjustColumnWidths(JTable table){
  TableColumnModel model=table.getColumnModel();
  for (int column=0; column < table.getColumnCount(); column++) {
    TableColumn tc=model.getColumn(column);
    int width=tc.getWidth();
    for (int row=0; row < table.getRowCount(); row++) {
      Component comp=table.prepareRenderer(table.getCellRenderer(row,column),row,column);
      width=Math.max(width,comp.getPreferredSize().width);
    }
    tc.setPreferredWidth(width);
  }
}

private void adjustColumnWidths ( JTable table ) { TableColumnModel model = table . getColumnModel ( ) ; for ( int column = 0 ; column < table . getColumnCount ( ) ; column ++ ) { TableColumn tc = model . getColumn ( column ) ; int width = tc . getWidth ( ) ; for ( int row = 0 ; row < table . getRowCount ( ) ; row ++ ) { Component comp = table . prepareRenderer ( table . getCellRenderer ( row , column ) , row , column ) ; width = Math . max ( width , comp . getPreferredSize ( ) . width ) ; } tc . setPreferredWidth ( width ) ; } }
adjust the column widths of a table based on the table contents .
