Title: Optimal Jtable size java
Tags: java;swing;jtable;pixel;text-size
Question: I have written a program that reads a csv file and put it into a jTable that will be displayed in a seperate jFrame as a preview. Everything works fine but I would like to use the optimal size for the jTable in the preview frame.
So how to do it? Because if for example I choose another font for the text, the column would be wider or smaller? Is there a method to get the text-length in pixels (size)?`
The class looks like this:
http://pastebin.com/Gqz10pci
Thanks
Comment: Consider `JDialog` rather than [multiple frames](http://stackoverflow.com/a/9554657/230513).

Comment: Look at [Table Column Adjuster](http://tips4java.wordpress.com/2008/11/10/table-column-adjuster/)

Comment: What exactly do you mean with *"optimal size"*? Do you mean actual code, like a `Component`'s `.setPreferredSize()` method? Or you mean it as *"to use the available screen-space more appropriately"* (which is usually subjective)?

Here is the accepted answer: JTable implements Scrollable, and the preferredViewportSize is arbitrarily set to Dimension(450, 400). That size is obtained by wrapping the table in a JScrollPane and invoking pack() on the enclosing Window. Mindful of some important caveats, you can use setPreferredScrollableViewportSize() to change the default size. A multiple of getRowHeight() may be a suitable choice for the height.


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 3, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2012:00:11/07/12', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<java><swing><jtable><pixel><text-size>', 'Title': 'Optimal Jtable size java', 'ViewCount': 449, 'id': 13270664, 'user_AboutMe': '<p>Full stack developer, loves swift and node.js</p>\n', 'user_AccountId': 1972656.0, 'user_DisplayName': 'Kingalione', 'user_Reputation': 4167.0, 'user_Views': 556.0, 'user_id': 1771213.0}}