Search code examples
javaswinguser-interfacegridbaglayout

How to make a Java GridBagLayout with six rows and 2 columns


I'm tired with Java GridBagLayout. I want to create a panel that looks like the one shown in the picture below, but I couldn't get it. I'm unable to position the left panel, and I failed to make panels long. The width does not increase when I set gridWidth. I can't use any GUI builders for that. I want to get a layout like the picture below.

enter image description here

This is the unsuccessful code:

public class gui3 extends Frame{
    Panel p1,p2,p3,p4,p5,p6,p7,pmain;
    gui3(){

    setVisible(true);
    setSize(500,500);
    setTitle(" Calculator ");


        GridBagLayout gb1=new GridBagLayout();
        GridBagConstraints gbc=new GridBagConstraints();
        setLayout(gb1);

        p1=new Panel();
        p1.setBackground(Color.BLACK);
        gbc.gridx=5;
        gbc.gridy=0;
        gbc.gridwidth=3;
        //gbc.weightx =0.5;
        gbc.fill=GridBagConstraints.HORIZONTAL;
        add(p1,gbc);

        p2=new Panel();
        p2.setBackground(Color.BLUE);
        gbc.gridx=0;
        gbc.gridy=1;
        gbc.gridwidth=2;
       // gbc.weightx = 1;
        gbc.fill=GridBagConstraints.HORIZONTAL;
        add(p2,gbc);

        p3=new Panel();
        p3.setBackground(Color.GREEN);
        gbc.gridx=0;
        gbc.gridy=2;
        gbc.gridwidth=2;
      //  gbc.weightx = 1;
        gbc.fill=GridBagConstraints.HORIZONTAL;
        add(p3,gbc);

        p4=new Panel();
        p4.setBackground(Color.cyan);
        gbc.gridx=0;
        gbc.gridy=3;
        gbc.gridwidth=2;
      //  gbc.weightx = 1;
        gbc.fill=GridBagConstraints.HORIZONTAL;
        add(p4,gbc);

        p5=new Panel();
        p5.setBackground(Color.RED);
        gbc.gridx=0;
        gbc.gridy=4;
        gbc.gridwidth=2;
     //   gbc.weightx = 1;
        gbc.fill=GridBagConstraints.HORIZONTAL;
        add(p5,gbc);

        p6=new Panel();
        p6.setBackground(Color.pink);
        gbc.gridx=0;
        gbc.gridy=5;
        gbc.gridwidth=2;
       // gbc.weightx = 1;
       // gbc.fill=GridBagConstraints.HORIZONTAL;
        add(p6,gbc);

        p7=new Panel();
        p7.setBackground(Color.yellow);
        gbc.gridx=6;
        gbc.gridy=0;
        gbc.gridheight=6;
     //   gbc.weightx = 1;
        gbc.fill=GridBagConstraints.HORIZONTAL;
        add(p7,gbc);


    }

}

Solution

  • You mean something like...

    Layout

    So basically, you can use GridBagConstraints#gridheight (or gridwidth) to set the number of grid cells that a component will span across

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class TestLayout {
    
        public static void main(String[] args) {
            new TestLayout();
        }
    
        public TestLayout() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                        ex.printStackTrace();
                    }
    
                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new GridBagLayout());
    
                    GridBagConstraints gbc = new GridBagConstraints();
                    gbc.insets = new Insets(4, 4, 4, 4);
                    gbc.gridx = 0;
                    gbc.weightx = 1;
                    gbc.gridy = 0;
                    gbc.fill = GridBagConstraints.HORIZONTAL;
                    frame.add(createPane(Color.RED), gbc);
                    gbc.gridy++;
                    frame.add(createPane(Color.GREEN), gbc);
                    gbc.gridy++;
                    frame.add(createPane(Color.BLUE), gbc);
                    gbc.gridy++;
                    frame.add(createPane(Color.CYAN), gbc);
                    gbc.gridy++;
                    frame.add(createPane(Color.MAGENTA), gbc);
                    gbc.gridy++;
                    frame.add(createPane(Color.ORANGE), gbc);
                    gbc.gridy++;
                    frame.add(createPane(Color.PINK), gbc);
    
                    gbc.gridx++;
                    gbc.weightx = 0;
                    gbc.gridy = 0;
                    gbc.weighty = 1;
                    gbc.gridheight = GridBagConstraints.REMAINDER;
                    gbc.fill = GridBagConstraints.VERTICAL;
                    frame.add(createPane(Color.YELLOW), gbc);
    
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public JPanel createPane(Color color) {
            JPanel pane = new JPanel(){ 
    
                @Override
                public Dimension getPreferredSize() {
                    return new Dimension(50, 50);
                }
    
            };
            pane.setBackground(color);
            return pane;
        }
    
    }
    

    Have a look at How to Use GridBagLayout for more details