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

  • Home
  • SEARCH
  • 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 9146491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:45:49+00:00 2026-06-17T10:45:49+00:00

I’ve been playing around with WindowBuilder Pro for Java Swing layouts, and encountered some

  • 0

I’ve been playing around with WindowBuilder Pro for Java Swing layouts, and encountered some strange behavior that I was eventually able to reduce down to a SSCCE. Consider the source code below:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class WBPTest2 {
    private JFrame frame;
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    WBPTest2 window = new WBPTest2();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    public WBPTest2() {
        initialize();
    }
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel();
        panel.setBackground(Color.RED);
        frame.getContentPane().add(panel, BorderLayout.CENTER);
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

        JPanel panel_1 = new JPanel();
        panel_1.setBackground(Color.GREEN);
        panel_1.setMaximumSize(new Dimension(32767, 100));
        panel.add(panel_1);
        panel_1.setLayout(new BoxLayout(panel_1, BoxLayout.Y_AXIS));

        JPanel panel_2 = new JPanel();
        panel_2.setBackground(Color.BLUE);
        panel_2.setMaximumSize(new Dimension(32767, 100));
        panel.add(panel_2);
        panel_2.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
    }
}

It gives the following layout:

enter image description here

For whatever reason, panel_1, the green one, floats to the right. Playing with some of the parameters in the code, I can only find two ways to completely kill the gap to the left of the green panel:

  • change the hgap and vgap in the panel_2 constructor to 0.

OR

  • change the minimumSize of panel_2 to (0,0).

Do either of those, and we get this:

enter image description here

Now, the question is, why on earth would either the minimumSize or the hgap for the FlowLayout in panel_2 have anything to do with preventing panel_1 from filling all the way to the left of its parent? What if I happened to want a minimumSize greater than zero, and an hgap greater than zero, for my FlowLayout in panel_2? How then would I eliminate the gap to the left of my BoxLayout in panel_1?

No matter what I do to panel_1, I cannot make it fill the whole width of its parent container (unless I edit panel_2 as described above). Ironically, if I set the alignmentX for panel_1 to RIGHT_ALIGNMENT, it actually floats to the left. But the gap still exists (just now on the right). As I fill panel_1 with content and increase its size, the gap to its right grows smaller and smaller, but never completely goes away, frustratingly.

This doesn’t just happen when panel_2 is a FlowLayout… it also happens with JScrollPane and many other types… types which don’t usually have the hgap parameter, which means the only way to fix the gap to the left of panel_1 is to change the new container’s minimumSize to 0, which again seems silly and unrelated, and most importantly may not be my design intent.

  • 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-06-17T10:45:51+00:00Added an answer on June 17, 2026 at 10:45 am

    Set alignmentX on panels 1 and 2 and it fills the screen.

    It appears the two different layout managers cause the panel to set different alignment defaults.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have been unable to fix a problem with Java Unicode and encoding. The
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.

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.