Search code examples
javaswing

JavaSwing GriBagLayout Placeing Problems


I had tried many ways to put the components on top of the panel, but they kept standing in the middle of the panel.

private void setWestPanel() {
        westPanel = new JPanel();
        westPanel.setBackground(Color.GRAY);
        westPanel.setLayout(new GridBagLayout());
        westPanel.setPreferredSize(new Dimension(100, 100));
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(5, 5, 5, 5);
        gbc.fill = GridBagConstraints.HORIZONTAL;

        JPanel westSubPanel = new JPanel();

        solveBtn = new JButton("Giải");
        resetBtn = new JButton("Xóa");
        exitBtn = new JButton("Thoát");

        solveBtn.setFocusable(false);
        resetBtn.setFocusable(false);
        exitBtn.setFocusable(false);

        solveBtn.setMnemonic('G');
        resetBtn.setMnemonic('X');
        exitBtn.setMnemonic('T');

        solveBtn.addActionListener(this);
        resetBtn.addActionListener(this);
        exitBtn.addActionListener(this);

        gbc.gridx = 0;
        gbc.gridy = 1;
        westPanel.add(solveBtn, gbc);

        gbc.gridx = 0;
        gbc.gridy = 2;
        westPanel.add(resetBtn, gbc);

        gbc.gridx = 0;
        gbc.gridy = 3;
        gbc.gridheight = GridBagConstraints.REMAINDER;
        westPanel.add(exitBtn, gbc);

        add(westPanel, BorderLayout.WEST);
    }

I want components to stand in the middle of the panel. I had tried gbc.anchor, but it didn't work.

Current:

https://i.sstatic.net/ocOEp.png

What I expecting:

https://i.sstatic.net/DD2ON.png


Solution

  • ... but they kept standing in the middle of the panel.

    Read the section from the Swing tutorial on How to Use GridBagLayout for information on the various constraints used by the GridBagLayout.

    The weightx/weighty constraint controls this behaviour. By default the components will be centered. If you don't want this then you need to have a component with a non-zero constraint.

    So in this case you would need to create a dummy component and add it to the end of the panel:

    JLabel dummy = new JLabel(" ");
    gbc.gridy = 4;
    gbc.weighty = 1.0;
    westPanel.add(dummy, gbc);
    

    Now all the extra vertical space goes to the last component. The components will still be centered horizontally.

    Or, another option is to use a "wrapper" panel:

    JPanel wrapper = new JPanel( new BorderLayout() );
    wrapper.add(westPanel, BorderLayout.PAGE_START);
    add(wrapper, BorderLayout.LINE_START);
    //add(westPanel, BorderLayout.WEST);
    

    Now the "westPanel" will be displayed at the top of the BorderLayout.

    Or maybe use a BoxLayout with "glue" at the end. Read the tutorial. Each layout manager has its own rules. Understanding the rules allows you to nest panels with different layout mangers to achieve your desired layout.

    Nested layouts can simplify the code. For example you could also use a GridLayout on the westPanel and add this panel to the wrapper panel. Then you don't need to worry about the gridx/y constraints.