javaswinglayout-managerboxlayout

setLayout() to BoxLayout causes JPanels to disappear


This is my first post so I apologize in advance if I do not include all the information I need to fully describe my problem.

I'm having trouble displaying a JFrame as I want to due to setbacks I'm having while using BoxLayout. I want to create use box layout horizontally to create two sections, and each of those sections will then use BoxLayout again (both vertically). However, when I try to set the layout of a JPanel to BoxLayout, the panel disappears. Furthermore, the preferred dimensions that I have chosen do not correlate to the values I have given them. Instead of being split into 67/33, the split is rather 85/15.

I have tried changing the layout of the JPanels and have had very little progress. Additionally, I have tried playing with the dimensions, and though I can get a visual I am okay with, I'm still wondering why my 67/33 split does not work in theory.

Below is my code:

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

public class test {

    public static void beginPoS() {
        // instantiating the opening frame
        JFrame openingPage = new JFrame("Point of Sales Simulation");
        openingPage.setSize(750, 600);
        openingPage.setVisible(true);
        openingPage.setLocationRelativeTo(null);
        openingPage.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        openingPage.setResizable(false);
        Container contents = openingPage.getContentPane();
        contents.setLayout(new BorderLayout());

    JPanel overall = new JPanel();
    overall.setLayout(new BoxLayout(overall, BoxLayout.X_AXIS));

    JPanel panel1 = new JPanel();
    panel1.setBackground(Color.RED);
    panel1.setPreferredSize(new Dimension(300, 600));
    panel1.setLayout(new BoxLayout(panel1, BoxLayout.Y_AXIS));

    JPanel panel2 = new JPanel();
    panel2.setLayout(new BoxLayout(panel2, BoxLayout.Y_AXIS));
    panel2.setBackground(Color.GREEN);
    panel2.setPreferredSize(new Dimension(50, 600));

    overall.add(panel1);
    overall.add(panel2);

    openingPage.add(overall);

    }

    public static void main(String[] args) {
        beginPoS();
    }

}

Solution

  • Multiple issue:

    panel1.setLayout(new BoxLayout(panel1, BoxLayout.Y_AXIS));
    
    JPanel panel2 = new JPanel();
    panel2.setLayout(new BoxLayout(panel2, BoxLayout.Y_AXIS));
    

    The Box layout doesn't appear to like that you have set the layout of the child panels and have not entered any components.

    So the preferred size of each child panel is (0, 0). So it can't grow.

    Delete the above two setLayout(...) statements.

    Instead of being split into 67/33, the split is rather 85/15.

    Well you have a typo. One panel has a width of 50, the other 300.

    The BoxLayout will first allocate the preferred space. Since you hardcoded the size of your frame to 750, you will have roughly 400 pixels of space left over. The BoxLayout will then allocation 200 pixels to each panel to fill the available space.

    openingPage.setSize(750, 600);
    openingPage.setVisible(true);
    openingPage.setLocationRelativeTo(null);
    openingPage.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    openingPage.setResizable(false);
    

    The above code should be executed AFTER all components have been added to the frame and the order is important. It should be more like:

    openingPage.setResizable(false);
    //openingPage.setSize(750, 600);
    openingPage.pack();
    openingPage.setVisible(true);
    openingPage.setLocationRelativeTo(null);
    openingPage.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    

    Normally you should use pack() instead of setSize(...) this layout manager will display the components at their preferred size. You want to set the resizable property before packing the frame since this will affect the border size of the frame. The pack method needs that information.

    I suggest you look at the demo code from the Swing tutorial on Layout Manager. The code will show you how to better structure your classes so the GUI is create on the Event Dispatch Thread (EDT).