Sizing of a JTable using GridBagLayout

Posted on 2006-03-28
Last Modified: 2012-08-13


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);
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.
Question by:ShamD
    LVL 30

    Expert Comment

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

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

    Author Comment

    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));

    LVL 30

    Accepted Solution

    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.

    Author Comment

    That worked.
    LVL 30

    Expert Comment

    Any reason for a B-grade?

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    Title # Comments Views Activity
    sumDigits  challenge 7 37
    count8 challlenge 13 63
    powerN  challenge 3 30
    HashMap Vs TreeMap 12 28
    INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
    Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
    This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
    This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

    794 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now