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 6988695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:06:55+00:00 2026-05-27T19:06:55+00:00

In the example below I have a JPanel with BoxLayout which contains another JPanel

  • 0

In the example below I have a JPanel with BoxLayout which contains another JPanel with GridBagLayout.

By adding a vertical glue I expect the contents of the inner panel to be “glued” to the top of the outer panel.

But I see the label “A” in the center. If I remove GridBagLayout, A is shown at the top.
Why does this happen?

import javax.swing.*;
import java.awt.*;

public class Test {
    public static void main(String[] args) {
        JPanel contentPane = new JPanel();        

        JFrame frame = new JFrame();
        frame.setContentPane(contentPane);

        contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));

        JPanel gridBagPanel = new JPanel();
        gridBagPanel.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.NONE;

        gridBagPanel.add(new JLabel("A"), gbc);
        contentPane.add(gridBagPanel);
        contentPane.add(Box.createVerticalGlue());

        frame.setSize(800, 200);
        frame.setVisible(true);
    }
}
  • 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-27T19:06:55+00:00Added an answer on May 27, 2026 at 7:06 pm

    Because the default value of the anchor property of GridBagConstraints is CENTER. If there is more space in the inner panel than the preferred size of the JLabel, the JLabel would be centered with respect to the available space in the inner panel (since you set fill to NONE).

    You can use the anchor value NORTHWEST to glue the label to the top of the panel:

    GridBagConstraints gbc = new GridBagConstraints();
    gbc.fill = GridBagConstraints.NONE;
    gbc.anchor = GridBagConstraints.NORTHWEST;
    gbc.weighty = 1;
    gbc.weightx = 1;
    

    You also need to set the weights per Java documentation: “Unless you specify at least one non-zero value for weightx or weighty, all the components clump together in the center of their container. This is because when the weight is 0.0 (the default), the GridBagLayout puts any extra space between its grid of cells and the edges of the container.“

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array which contains serialized data similar to the example below. Array
Class SampleFiveA extends JPanel. This contains textfields, one below the other, each of which
I have below example made in jsfiddle which works perfectly in Firefox 6 and
In the example below I have a ListBox with dozens of font names in
How can I select the good method (I have in the example below show
Below I have a very simple example of what I'm trying to do. I
I have an example function below that reads in a date as a string
Let's consider the below example. There, I have: target MAIN calls target t and
okay, so i have a text file with example content below line1with some random
I have example of code below. <script type=text/javascript src=assets/scripts/somescript.php>. </script> So, will my browser

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.