how to resize columns of a JTable to automatically fit content

honkinamiland
honkinamiland used Ask the Experts™
on
hi,
i have a jtable, that has three columns. in the first two there is only a number, the last one contains a string. right now every column hast the same size and there is not enough space for the string. how can i make the table to decrease the the with for the first two columns?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
Try setPreferredWidth (from TableColumn) for the first two columns.

Commented:
and doLayout in JTable (sorry.  That should all have been in one post)

Author

Commented:
ok, i needed also to set the max width to make it work, but that's it.
thanks

Commented:
Just in case you need more, there's a good example here:

http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#custom

It's the example after the "Version note" after this heading.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial