Skip to content
Advertisement

GridBagLayout strange behaviour

I wrote this simple class to test GridBagLayout.

My scope was to put some buttons in diagonal into panel, using grid bag layout and setting gridx and gridy, but I have a strange behaviour. If I put gridwidth = 2 on button "2", button "3" will be drawn under button labeled "2".

My class is a simple demo, but I cant figure out what is wrong with it Guess Why?

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Test extends JFrame {

    private JPanel panel;
    private JButton b1;
    private JButton b2;
    private JButton b3;
    private JButton b4;
    private JButton b5;
    private JLabel label1;

    public Test() {

        panel = new JPanel();
        panel.setLayout(new GridBagLayout());
        panel.setBackground(Color.DARK_GRAY);
        GridBagConstraints gbc = new GridBagConstraints();
        b1 = new JButton("1");
        gbc.gridx = 0;
        gbc.gridy = 0;
        panel.add(b1, gbc);

        b2 = new JButton("2");
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 1;

        gbc.gridwidth = 2;

        panel.add(b2, gbc);

        gbc = new GridBagConstraints();
        b3 = new JButton("3");
        gbc.gridx = 2;
        gbc.gridy = 2;
        panel.add(b3, gbc);

        gbc = new GridBagConstraints();
        b4 = new JButton("4");
        gbc.gridx = 3;
        gbc.gridy = 3;
        panel.add(b4, gbc);

        gbc = new GridBagConstraints();
        b5 = new JButton("5");
        gbc.gridx = 1;
        gbc.gridy = 4;
        gbc.gridwidth = 3;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        panel.add(b5, gbc);

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocation(500, 400);
        this.setSize(800, 300);
        this.setTitle("Frame principale dell'applicazione");
        this.setResizable(true);

        getContentPane().add(panel, BorderLayout.CENTER);

        this.setVisible(true);
    }

    public static void main(String[] args) {
        Test mioFrame = new Test();
    }
}

Advertisement

Answer

I created the following unusual GUI

Button GUI

Here’s what I did.

  1. You only need one instance of GridBagConstraints.

  2. I made all of the gridwidth values 1.

  3. I set gbc.fill to GridBagConstraints.NONE.

Here’s the code I ran.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class GridBagLayoutTestGUI extends JFrame {

    private static final long serialVersionUID = 1L;
    
    private JPanel panel;
    private JButton b1;
    private JButton b2;
    private JButton b3;
    private JButton b4;
    private JButton b5;
    private JLabel label1;

    public GridBagLayoutTestGUI() {
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setTitle("Frame principale dell'applicazione");

        this.panel = createMainPanel();
        getContentPane().add(panel, BorderLayout.CENTER);

        this.pack();
        this.setLocationByPlatform(true);
        this.setVisible(true);
    }

    private JPanel createMainPanel() {
        JPanel panel = new JPanel();
        panel.setLayout(new GridBagLayout());
        panel.setBackground(Color.DARK_GRAY);
        GridBagConstraints gbc = new GridBagConstraints();
        
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.fill = GridBagConstraints.NONE;
        b1 = new JButton("1");
        panel.add(b1, gbc);
        
        gbc.gridx = 1;
        gbc.gridy = 1;
        b2 = new JButton("2");
        panel.add(b2, gbc);

        gbc.gridx = 2;
        gbc.gridy = 2;
        b3 = new JButton("3");
        panel.add(b3, gbc);

        gbc.gridx = 3;
        gbc.gridy = 3;
        b4 = new JButton("4");
        panel.add(b4, gbc);

        gbc.gridx = 4;
        gbc.gridy = 4;
        b5 = new JButton("5");
        panel.add(b5, gbc);
        
        return panel;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new GridBagLayoutTestGUI();
            }
        });
    }
}
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement