How do I dynamically resize components according to framesize in a GUI?

Posted on 2007-10-03
Last Modified: 2013-11-23
I am trying to develop an application that has to create JTables from database queries on the fly. Creating the table is not a problem, but it has been requested that the form be resizable. I have the table set into a JScrollPane. How do I
1). resize the table without resizing the scrollpane?
2). resize the scrollpane to fit the resized frame?

This table shares the form with a lot of other components and I am looking at using either a null layout or a gridbag layout
Question by:Cmerf486
    LVL 86

    Expert Comment

    The best thing to do is to use a layout manager or you'll have to do all resizing calculations yourself. Using a layout manager means it should all happen automatically

    Author Comment

    I don't think I asked the right question. How do I resize the components in my frame whenever a user resizes the frame itself? Using the gridbag layout, the components stayed centered, but did not resize.
    LVL 13

    Accepted Solution

    Using the GridBagConstraints you can tell the layout manager which components should take up the extra space.

    Use GridBagConstraints.fill to achieve this. Fill can be set to GridBagConstraints.NONE (default), GridBagConstraints.HORIZONTAL, GridBagConstraints.VERTICAL, GridBagConstraints.BOTH

    Author Comment

    Cool. Thanks Bart_Cr. That helped, I guess I also needed to give the components a weightx and weighty value on the gridbagconstraints.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
    This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

    761 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

    8 Experts available now in Live!

    Get 1:1 Help Now