Search code examples
javaswingconstraintslayout-managergridbaglayout

Swing : GridBagLayout adding components not the expected way


I'm just trying to use the GridBagLayout following this tutorial.

I can't get what I want to have.

I would want each Panel to take as much space as possible but it is not the case, I really tried everything, following the steps in the Oracle Docs

But the result is always the same :

GBC

I really do not understand why the layout is not working.

I tried changing the layout of the JFrame, setting the panel as ContentPane(), tried to add the constraints found on both websites, nothing seems to work.

Your help will is appreciated.


Full Code

public class Monitor extends JFrame{

    // Menu
    private JMenuBar bar = new JMenuBar();
    private JMenu file = new JMenu("File");
    private JMenuItem open = new JMenuItem("Open");
    private JMenuItem exit = new JMenuItem("Exit");

    private JPanel mainPanel = new JPanel();

    private JPanel secondaryPanel;
    private JPanel explorerPanel, tagPanel;

    public Monitor(){
        setTitle("");
        setSize(750,600);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setResizable(false);


        initComps();
    }

    public void initComps(){

        explorerPanel = new JPanel();
        explorerPanel.setBackground(Color.BLACK);
        explorerPanel.setPreferredSize(new Dimension(250,300));

        tagPanel = new JPanel();
        tagPanel.setBackground(Color.yellow);
        tagPanel.setPreferredSize(new Dimension(250, 300));

        secondaryPanel = new JPanel();
        secondaryPanel.setBackground(Color.blue);
        secondaryPanel.setPreferredSize(new Dimension(500, 600));

        mainPanel = new JPanel();
        mainPanel.setPreferredSize(new Dimension(750, 600));
        mainPanel.setLayout(new GridBagLayout());

        GridBagConstraints gbc = new GridBagConstraints();

        gbc.gridx = 0 ; gbc.gridy = 0 ; gbc.gridwidth = 1 ; gbc.gridheight = 1;
        mainPanel.add(explorerPanel, gbc);

        gbc.gridx = 1 ; gbc.gridy = 0 ; gbc.gridwidth = 2 ; gbc.gridheight = 2;
        mainPanel.add(secondaryPanel, gbc);

        gbc.gridx = 0 ; gbc.gridy = 1 ; gbc.gridwidth = 1 ; gbc.gridheight = 1;
        mainPanel.add(tagPanel, gbc);

        this.setContentPane(mainPanel);

        // Menu Bar
        file.add(open);

        exit.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        exit.setMnemonic('q');
        file.add(exit);

        bar.add(file);
        setJMenuBar(bar);
    }

    public static void main(String[] args) {
        new Monitor().setVisible(true);

    }
}

EDIT :

When adding

gbc.weightx = 1;
gbc.weighty = 1;

Result

GBC


Solution

  • Your problem comes from your dimensions.

    Don't use "preferred" only. Use minimum as well.

    explorerPanel = new JPanel();
    explorerPanel.setBackground(Color.BLACK);
    explorerPanel.setMinimumSize(new Dimension(250, 300));
    explorerPanel.setPreferredSize(new Dimension(250, 300));
    
    tagPanel = new JPanel();
    tagPanel.setBackground(Color.yellow);
    tagPanel.setMinimumSize(new Dimension(250, 300));
    tagPanel.setPreferredSize(new Dimension(250, 300));
    
    secondaryPanel = new JPanel();
    secondaryPanel.setBackground(Color.blue);
    secondaryPanel.setMinimumSize(new Dimension(500, 600));
    secondaryPanel.setPreferredSize(new Dimension(500, 600));
    
    mainPanel = new JPanel();
    mainPanel.setPreferredSize(new Dimension(750, 600));
    mainPanel.setMinimumSize(new Dimension(750, 600));
    mainPanel.setLayout(new GridBagLayout());
    

    This worked in my test:enter image description here