Search code examples
javaswingawtgridbaglayout

Nested JPanels and GridBagLayout Not "Packing" Components


I've attached a screenshot for which the following Border legend applies:

Yellow = JPanel with BorderLayout

Blue = JPanel with GridBagLayout

Fuchsia = JPanel with FlowLayout

There are two panels not blocked out in colors that warrant mentioning:

1) The title panel where the word "Primary" is displayed; this panel is at BorderLayout.NORTH in "Yellow" panel.

2) The image panel where the image of the device is located; this panel is a sibling to "Fuchsia"

Nested Layout Problem

"Blue" is at BorderLayout.CENTER in "Yellow" while "Fuchsia" and the image panel are given the following constraints:

GridBagConstraints c = new GridBagConstraints();

c.weightx = 1.0;
c.weighty = 1.0;
c.gridx = 0;
c.gridy = 0;
c.anchor = GridBagConstraints.NORTHWEST;
c.insets = new Insets(0, 10, 0, 0);
c.fill = GridBagConstraints.BOTH;

//"Blue".add(imagePanel, c);

c.weighty = 0.80;       
c.gridy = 1;
c.fill = GridBagConstraints.HORIZONTAL;


//"Blue".add("Fuchsia", c);

As you can probably tell from the image, I'm trying to get rid of the "wasted" space in "Blue" right below "Fuchsia". I don't seem to be able to do it with GridBagConstraints, so am I just using the wrong LayoutManager? It looks to me like "Blue", who is at CENTER in the BorderLayout is just giving each child JPanel half of the available space and reserving the remainder space instead of contracting upward. What am I missing here? Is this simply a matter of setting a preferred or maximum size on "Fuchsia"? it doesn't seem like that will get me where I want to be, since the border around "Fuchsia" (which is covered by my color coding) is where I want the end of the component to be.


Solution

  • LAYOUT SNAPSHOT

    Have a look at this output, from this code example :

    import java.awt.*;
    import javax.swing.*;
    
    public class LayoutTest
    {
        private void displayGUI()
        {
            JFrame frame = new JFrame("Layout Test");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            JPanel contentPane = new JPanel();
            contentPane.setOpaque(true);
            contentPane.setBackground(Color.YELLOW);
            contentPane.setLayout(new BorderLayout(2, 2));
    
            JPanel topPanel = new JPanel();
            topPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
            JLabel headingLabel = new JLabel("Primary");
            topPanel.add(headingLabel);
            contentPane.add(topPanel, BorderLayout.PAGE_START);
    
            JPanel centerPanel = new JPanel();
            centerPanel.setOpaque(true);
            centerPanel.setBackground(Color.BLUE);
            centerPanel.setLayout(new GridBagLayout());
    
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.anchor = GridBagConstraints.FIRST_LINE_START;
            gbc.fill = GridBagConstraints.BOTH;
            gbc.weightx = 1.0;
            gbc.weighty = 0.2;
            gbc.gridx = 0;
            gbc.gridy = 0;
    
            JPanel imagePanel = new JPanel();   
            JLabel imageLabel = null;
            try
            {
                imageLabel = new JLabel(
                                new ImageIcon(
                                    new java.net.URL(
                                        "http://pscode.org/"
                                        + "tame/screenshot/"
                                        + "landscape/slider1.gif")));
            }
            catch(Exception e)  
            {
                e.printStackTrace();
            }
            imagePanel.add(imageLabel);
            centerPanel.add(imagePanel, gbc);
    
            JPanel detailsPanel = new JPanel();
            detailsPanel.setOpaque(true);
            detailsPanel.setBackground(Color.WHITE);
            detailsPanel.setBorder(
                            BorderFactory.createEmptyBorder(
                                                  5, 5, 5, 5));
            detailsPanel.setLayout(new GridLayout(0, 1, 5, 5));
    
            JLabel statusLabel = new JLabel("Chassis Status : ");
            JLabel usageLabel = new JLabel("Bandwidth Usage : ");
            JLabel fanLabel = new JLabel("Fan Status : ");
    
            detailsPanel.add(statusLabel);
            detailsPanel.add(usageLabel);
            detailsPanel.add(fanLabel);
    
            gbc.fill = GridBagConstraints.BOTH;
            gbc.weighty = 0.8;
            gbc.gridx = 0;
            gbc.gridy = 1;
            gbc.gridheight = 3;
            centerPanel.add(detailsPanel, gbc);
    
            contentPane.add(centerPanel, BorderLayout.CENTER);
    
            frame.setContentPane(contentPane);
            frame.pack();
            frame.setLocationByPlatform(true);
            frame.setVisible(true);
        }
    
        public static void main(String... args)
        {
            SwingUtilities.invokeLater(new Runnable()
            {
                public void run()
                {
                    new LayoutTest().displayGUI();
                }
            });
        }
    }