Search code examples
javaswingjpanelgridbaglayout

Not able to add 3 JPanels to a main panel


I have 3 JPanels and I want to place them all in one JPanel. I used the GridBagLayout for the main panel. But only one panel is getting added. Why might this be?

    gblayout=new GridBagLayout();
    gbc=new GridBagConstraints();
    panel1Customizer();
    panel2customizer();
    panel3Customizer();
    setLayout(gblayout);
    gbc.fill=GridBagConstraints.HORIZONTAL;
    gbc.anchor=GridBagConstraints.NORTHWEST;
    gbc.weightx=1;
    gbc.weighty=1;
    gbc.gridheight=GridBagConstraints.REMAINDER;
    add(panel1, gbc);
    add(panel2, gbc);
    gbc.gridwidth=GridBagConstraints.REMAINDER;
    add(panel3, gbc);

The customizer methods are ones which add items into these panels.


Solution

  • I am not sure but I think you need to add a GridBagConstraints to your GridBagLayout. Try look at this site to get the idea on how to work with GridBagLayout: link

    Or maybe just use another Layout for your JFrame, maybe BorderLayout or GridLayout to arrange your Panels correctly