Search code examples
javaswinglayout-managergridbaglayout

GridBagLayout in Java Swing


I have the following problem. I have a JPanel with GridBagLayout() set. I add another JPanel to it with the same layout manager. I have several different objects in it. The problem is that I can not set them so that the objects marked with No. 2 are in front of the object number 1. That is, objects number 2 should be first from the left side.

Below the code:

public MainPanel() {
    setLayout(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    SecondPanel secondPanel = new SecondPanel();
    gbc.insets = new Insets(10, 20, 10, 20);
    gbc.anchor = GridBagConstraints.WEST;
    gbc.fill = GridBagConstraints.LINE_END;
    gbc.weightx = 0.1;
    add(secondPanel , gbc);
}

class SecondPanel extends JPanel {
        public SecondPanel () {
        setLayout(new GridBagLayout());
        JPanel main = new JPanel(new GridBagLayout());

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(4, 4, 4, 4);

        gbc.gridwidth = GridBagConstraints.REMAINDER;
        gbc.anchor = GridBagConstraints.LINE_START;
        main.add(ope, gbc);
        gbc.anchor = GridBagConstraints.LINE_END;
        main.add(opeV, gbc);
        gbc.anchor = GridBagConstraints.LINE_START;
        main.add(am, gbc);
        gbc.anchor = GridBagConstraints.LINE_END;
        main.add(amV, gbc);
        gbc.anchor = GridBagConstraints.LINE_START;
        main.add(cry, gbc);
        gbc.anchor = GridBagConstraints.LINE_END;
        main.add(cryV, gbc);

        gbc.anchor = GridBagConstraints.BOTH;
        main.add(addButton);

        BufferedImage img = new BufferedImage(320, 240, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = img.createGraphics();
        g2d.setColor(Color.RED);
        g2d.drawLine(0, 0, 320, 240);
        g2d.drawLine(320, 0, 0, 240);
        g2d.dispose();

        mainL.setIcon(new ImageIcon(img));
        mainL.setBorder(new LineBorder(Color.RED));

        gbc = new GridBagConstraints();
        gbc.weightx = 1;
        gbc.gridwidth = 1;
        gbc.anchor = GridBagConstraints.WEST;
        add(mainL, gbc);

        add(main);

        setVisible(true);
    }
}

Screenshot :

Screenshot of mainPanel

EDIT : I solved problem, for first in this code was mistake add(mainL, gbc); should be main.add(mainL,gbc);. Secondly, I make two different panels from SecondPanel and to MainPanel separatly.


Solution

  • Stop, go read How to Use GridBagLayout and make sure you keep the JavaDocs for GridBagLayout and GridBagConstraints on hand.

    GridBagLayout is a, surprisingly, "grid" based layout, with flexibility.

    If you want to place one component on top of another, then you're going to have to tell the layout that both components share the same cell. This is where gridx and gridy are important

    So when you do something like...

    add(mainL, gbc);
    add(main);
    

    You're allowing the layout manager to make decisions about how best main should laid out, which isn't what you want, instead, you need to provide both components with the same gridx/gridy constraint, for example...

    Overlapped

    import java.awt.Color;
    import java.awt.EventQueue;
    import java.awt.Graphics2D;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    import java.awt.image.BufferedImage;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    import javax.swing.border.LineBorder;
    
    public class Test {
    
        public static void main(String[] args) {
            new Test();
        }
    
        public Test() {
            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.add(new SecondPanel());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        class SecondPanel extends JPanel {
    
            public SecondPanel() {
                setLayout(new GridBagLayout());
                JPanel main = new JPanel(new GridBagLayout());
                main.setBorder(new LineBorder(Color.GRAY));
    
                GridBagConstraints gbc = new GridBagConstraints();
                gbc.insets = new Insets(4, 4, 4, 4);
    
                gbc.gridwidth = GridBagConstraints.REMAINDER;
                gbc.anchor = GridBagConstraints.LINE_START;
                main.add(new JLabel("Ope"), gbc);
                gbc.anchor = GridBagConstraints.LINE_END;
                main.add(new JLabel("opeV"), gbc);
                gbc.anchor = GridBagConstraints.LINE_START;
                main.add(new JLabel("am"), gbc);
                gbc.anchor = GridBagConstraints.LINE_END;
                main.add(new JLabel("amV"), gbc);
                gbc.anchor = GridBagConstraints.LINE_START;
                main.add(new JLabel("cry"), gbc);
                gbc.anchor = GridBagConstraints.LINE_END;
                main.add(new JLabel("cryV"), gbc);
    
                gbc.anchor = GridBagConstraints.CENTER;
                main.add(new JButton("Add"), gbc);
    
                BufferedImage img = new BufferedImage(320, 240, BufferedImage.TYPE_INT_ARGB);
                Graphics2D g2d = img.createGraphics();
                g2d.setColor(Color.RED);
                g2d.drawLine(0, 0, 320, 240);
                g2d.drawLine(320, 0, 0, 240);
                g2d.dispose();
    
                JLabel mainL = new JLabel();
                mainL.setIcon(new ImageIcon(img));
                mainL.setBorder(new LineBorder(Color.RED));
    
                gbc = new GridBagConstraints();
                gbc.gridx = 0;
                gbc.gridy = 0;
                add(main, gbc);
                add(mainL, gbc);
            }
        }
    }
    

    I've left main opaque and added a border so you can see it. Also beware, that components are displaying in FILO (reverse) order

    GridBagLayout is an art form, one you just have to muck about with to come to grips with

    But this dont resolve my problem

    You mean something like...

    something like

    gbc = new GridBagConstraints();
    gbc.gridx = 0;
    gbc.gridy = 0;
    add(main, gbc);
    gbc.gridx++;
    add(mainL, gbc);