JTable column width example - setting the column width

Here is a quick example of how to set JTable column widths. I don't know if this is a perfect solution, but it is one possible solution:

for (int i=0; i<numColumnsInTableModel; i++) {
  TableColumn column = myJTable.getColumnModel().getColumn(i);
  if (i==0) column.setPreferredWidth(10);
  if (i==1) column.setPreferredWidth(50);
  if (i==2) column.setPreferredWidth(50);
  if (i==3) column.setPreferredWidth(10);
  if (i==4) column.setPreferredWidth(100);
  if (i==5) column.setPreferredWidth(10);
  if (i==6) column.setPreferredWidth(10);
  if (i==7) column.setPreferredWidth(10);
  if (i==8) column.setPreferredWidth(10);
  if (i==9) column.setPreferredWidth(10);
}

The basic implementation is this:

  1. For each column in your JTable table model (TableModel) ...
  2. Get a TableColumn reference from the JTable.
  3. Set the preferred width on that column.

Note that the preferred width values are relative values, not absolute positioning using pixels or anything like that.