File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes JTable and its size Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable and its size" Watch "JTable and its size" New topic

JTable and its size

Mihai Radulescu
Ranch Hand

Joined: Sep 18, 2003
Posts: 918

Hi I use a JTable with JScrollPane like this :

JScrollPane scrollPane = new JScrollPane(table);

where the table is my JTable but the resoult is a gigant table.For shrink it (make it smaller) I find two solution:
1.I set a preferred size for the frame and the table goes small
2.I set a prefferd size for the view port with : setPreferredScrollableViewportSize(..

The both solution require "hardcode" and I am not so happy with them.
My question is :
Thare are also other ways to resize the table without "hardcode" ?

Eric Snell

Joined: Jun 09, 2004
Posts: 28

Here is a class that will calculate the PreferredScrollableViewportSize (along with some other things). Hope you find it helpful.

Mihai Radulescu
Ranch Hand

Joined: Sep 18, 2003
Posts: 918

Ok it works,Thanks!
I agree. Here's the link:
subject: JTable and its size
It's not a secret anymore!