Monday, March 19, 2007

JTable Size Columns

I was trying to find a way to auto size columns of a JTable much like the functionality of excel. I found out that that was a real pain, so I decided to just set specific sizes for the columns. This is the code I came up with:


TableColumn column = null;
for (int i = 0; i < 5; i++) {
column = table.getColumnModel().getColumn(i);
if (i == 2) {
column.setPreferredWidth(100);
} else {
column.setPreferredWidth(50);
}
}

No comments: