Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top