posted 13 years ago
TableColumn has methods for setting the preferred, minimum and maximum widths. That leaves calculating the sizes based on the contents, and that's where the renderer can help you out.
Loop through the table, get a renderer and editor for each cell, get its component and get its preferred size:
I've chosen to use the widths of both the renderer and editor, and for selected cells with focus, as these tend to give the largest results. Now just use the maximum value per column as the minimum width for the entire column.
That leaves setting the auto resize mode for the table; if your minimum widths don't fit you'll run into problems if you have the auto resize mode set to anything but AUTO_RESIZE_OFF.
Oh, I forgot about the changing of values. Just override setValueAt of the JTable (so you'll have a JTable subclass, probably anonymous):