2

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

CosmicGiant
  • 6,275
  • 5
  • 43
  • 58
Kingalione
  • 4,237
  • 6
  • 49
  • 84
  • 3
    Look at [Table Column Adjuster](http://tips4java.wordpress.com/2008/11/10/table-column-adjuster/) – cubanacan Nov 07 '12 at 13:24
  • 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)? – CosmicGiant Nov 07 '12 at 13:33
  • Consider `JDialog` rather than [multiple frames](http://stackoverflow.com/a/9554657/230513). – trashgod Nov 07 '12 at 14:47

1 Answers1

2

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.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045