Java examples for Swing:JTable Column
column Fit JTable
//package com.java2s; import javax.swing.JTable; import javax.swing.table.DefaultTableModel; public class Main { public static void columnFit(JTable table) { DefaultTableModel model = (DefaultTableModel) table.getModel(); int rows = model.getRowCount(); int cols = model.getColumnCount(); for (int i = 0; i < cols; i++) { int max = 0; for (int j = 0; j < rows; j++) { Object obj = table.getValueAt(j, i); if (obj == null) continue; String tmp = obj.toString(); if (tmp.length() > max) max = tmp.length();/*from w w w . jav a2s .c o m*/ } table.getColumnModel().getColumn(i).setWidth(max + 75); table.getTableHeader().setResizingColumn( table.getColumnModel().getColumn(i)); } } }