Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7191491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:44:21+00:00 2026-05-28T19:44:21+00:00

I want to layout my JPane like so: ——- | | | | |

  • 0

I want to layout my JPane like so:

-------
|     |
|     |
|     |
-------
|     |
-------

This way, the top section is bigger/taller than the bottom section (the top section consists of another JPanel and uses the Graphics object to display an image, while the bottom section also consists of another JPanel but uses the Graphics object to draw some lines and text).

I’ve heard that the best way to do this was using the GridBagLayout and GridBagConstraints.

I’m trying to figure out the appropriate properties for the GridBagConstraints, and I’m having some difficulties. This is what I have so far…

For the top part, I have:

gridx = 0
gridy = 0
weighty = 1.0; // expand downwards, because the bottom should never expand in the Y direction
fill = GridBagConstraints.BOTH

For the bottom part, I have:

gridx = 0
gridy = 1
fill = GridBagConstraints.HORIZONTAL
anchor = GridBagConstraints.PAGE_END

Unfortunately, all the ends up happening is a large gray rectangle appears (I have a white background for the application) – no images load, no lines/text appear.

What should I do? What should I adjust?

I’ve read a few tutorials, but it just seems really confusing, I got it working in my first application, but now when I try to do this it just doesn’t seem to work for me.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T19:44:22+00:00Added an answer on May 28, 2026 at 7:44 pm

    In general, for gridbag layout

    • if you want a component scale, you must give its scale direction a weight, and any sizes (width/height) you set for that direction will be ignored by the layout manager.

    • If you don’t want a component scale, the component must have its size defined (if you want, you can dig into this topic in documents of java). In your case of the bottom panel, you need to give its, at least, a preferred height.

    This can work as your expectation

    pnlTop.setBackground(Color.WHITE);
    pnlBottom.setBackground(Color.BLUE);
    
    // Because you don't want the bottom panel scale, you need to give it a height.
    // Because you want the bottom panel scale x, you can give it any width as the
    // layout manager will ignore it.
    pnlBottom.setPreferredSize(new Dimension(1, 20));
    
    
    getContentPane().setLayout(new GridBagLayout());
    GridBagConstraints cst = new GridBagConstraints();
    cst.fill = GridBagConstraints.BOTH;
    cst.gridx = 0;
    cst.gridy = 0;
    cst.weightx = 1.0; // --> You miss this for the top panel
    cst.weighty = 1.0;
    getContentPane().add(pnlTop, cst);
    
    cst = new GridBagConstraints();
    cst.fill = GridBagConstraints.HORIZONTAL;
    cst.gridx = 0;
    cst.gridy = 1;
    cst.weightx = 1.0; // You miss this for the bottom panel
    cst.weighty = 0.0;
    getContentPane().add(pnlBottom, cst);
    

    Further more, if you want to use gridbag layout, I recommend you to try the painless-gridbag library http://code.google.com/p/painless-gridbag/ (I’m the author of that library). It doesn’t solve this problem for you (as your problem concerns managing component’s size in gridbag layout) but it will save you a lot of typing and make your code easier to maintain

    pnlBottom.setPreferredSize(new Dimension(1, 20));
    
    PainlessGridBag gbl = new PainlessGridBag(getContentPane(), false);
    gbl.row().cell(pnlTop).fillXY();
    gbl.row().cell(pnlBottom).fillX();
    gbl.done();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a layout in such a way that on top edittext
I want to have a 3x3 grid layout structured like this: _ _ _
I want to use JPanels like containers from top to bottom just like DIV
I want to code a simple form layout in flex. Something like the following:
I want to layout a button with Drawable image on the top of it
First let me tell you what I want to achieve. I want a layout
I want the following layout to appear on the screen: FieldName 1 [Field input
I want to create a Button otherthan creating it from xml layout.i want to
I want my website to change its layout according to the device positon. The
I want to use WebKit as the layout/rendering engine, and I want to code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.