We help IT Professionals succeed at work.

Sizing of a JTable using GridBagLayout

ShamD
ShamD asked
on
Medium Priority
1,462 Views
Last Modified: 2012-08-13

Hi,

I am displaying a JTable on the screen, but I cannot set the Default width or Height of the table.
No matter what I try, the JTable appears with the same height and width.
The Horizontal scrollbar appears and I can scroll across to see all the columns.

I want the table to appear at a fixed width - say 800 pixels wide. And the horizontal scrollbar only to appear if needed.

JTable table = new JTable(data, columnHeaders);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
container.add(new JScrollPane(table));


I am not sure if the Layout manager i am using is messing with this - prob not.

Can anyone tell me how to fix this please.
Sham
Comment
Watch Question

Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
>> container.add(new JScrollPane(table));

The size of the table will be controlled by the size of the scroll-pane.

Author

Commented:
Does that mean that if I set the size of the JScrollPane,
The table width should appear wider?

For example;
            JScrollPane f = new JScrollPane(table);
            f.setMinimumSize(new Dimension(500, 500));

Principal Technologist
CERTIFIED EXPERT
Commented:
Maybe yes - not sure as I have not tried it on GridBagLayout. Try removing any settings for the table width, height and just set it for the scroll-pane - see if that helps.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Thanks
That worked.
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
Any reason for a B-grade?
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.