JTable - How to size specific columns before it is displayed?

So here is the deal. I have this screen that when you select a few items from combo boxes, it then fires off to get data, and dynamically create the jtable. I uses this GroupableTableHeader code I found out on the net to display two rows in the table header. For each one column, the second row has two columns grouped under it. On the left most column, I list a bunch of string valued items. Some are quite long, about 40 to 60 characters in length. After creating the table, I get its font metrics, and take the longest string to display and get its string width. I then get the column model, get the column(0), and set the width. But when displayed, it never shows it at that width.

So I ask, how do I do this? Is there some event I need to fire after making this width change? I know when I dynamically add columns to the table, or row data, I have to fire the datastructure and other event (forgot what it is) for the table to somehow update itself.

Thanks.

[995 byte] By [buckman1] at [2007-9-30 4:22:32]
# 1
Did you try --.getColumn(0).setMaxWidth(35);
javalearner88 at 2007-7-1 12:48:23 > top of Java-index,Archived Forums,Socket Programming...
# 2
Yup. Tried that in many a places, no go.
buckman1 at 2007-7-1 12:48:23 > top of Java-index,Archived Forums,Socket Programming...
# 3

Put something similar to this is the JTable constructor:

for (int i = 0; i < numColumns; i++) {

TableColumn column = this.getColumnModel().getColumn(i);

column.setPreferredWidth(35);

}//end for

tajenkins at 2007-7-1 12:48:23 > top of Java-index,Archived Forums,Socket Programming...
# 4
Oh,int numColumns = getModel().getColumnCount();
tajenkins at 2007-7-1 12:48:23 > top of Java-index,Archived Forums,Socket Programming...
# 5

You need to try something like this. But you also need to do a comparison with the data in the cells.

TableColumn column = table.getColumnModel().getColumn(0);

TableCellRenderer headerRenderer = table.getTableHeader().getDefaultRenderer();

Component component = headerRenderer.getTableCellRendererComponent(null, column.getHeaderValue(), false, false, 0, 0);

column.setPreferredWidth(component.getPreferredSize().width);

kevinsikora at 2007-7-1 12:48:23 > top of Java-index,Archived Forums,Socket Programming...