Undesired outcome using GridBagLayout

48 views Asked by At

I've been trying to learn by Youtube videos, but there must be something I missing about GridBagLayout.

Basically what I expect is 4 buttons, one below another, with an height of 2 grids, but it seems that no matter what value I set gridheight, the buttons stay the same size

Compilable code :

package test;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JFrame;

public class Test{

 public static void main(String []args){
    System.out.println("Hello World");

    JFrame jay = new JFrame("test");
    JPanel jp = new JPanel(new GridBagLayout());
    GridBagConstraints gc = new GridBagConstraints();

    JButton j1 = new JButton("Button 1");
    JButton j2 = new JButton("Button 22222222222222");
    JButton j3 = new JButton("Button 333333");
    JButton j4 = new JButton("4");

    gc.gridx = 0; gc.gridy = 0;
    gc.insets = new Insets(5,10,5,10);
    gc.gridheight = 2; //This is where the problem is;
    gc.fill = GridBagConstraints.BOTH;
    jp.add(j1,gc);
    gc.gridx = 0; gc.gridy = 2;
    jp.add(j2,gc);
    gc.gridx = 0; gc.gridy = 4;
    jp.add(j3,gc);
    gc.gridx = 0; gc.gridy = 6;
    jp.add(j4,gc);
    jay.add(jp);
    jay.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jay.setSize(300,350);
    jay.setVisible(true);

 }
}

Also if someone explain me what determines the size of the grids? is it just the component size?

1

There are 1 answers

0
MadProgrammer On BEST ANSWER

gridheight specifies the number of rows that a component will expand, assuming that there are components in those rows (any empty row will automatically be sized to 0)

how would I go about achieving taller buttons then? adding blank components?

One possible solution is to use ipady which is added to the component's preferredSize property (you can also use ipadx for width)

enter image description here

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.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

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 TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gc = new GridBagConstraints();

            JButton j1 = new JButton("Button 1");
            JButton j2 = new JButton("Button 22222222222222");
            JButton j3 = new JButton("Button 333333");
            JButton j4 = new JButton("4");

            gc.insets = new Insets(5, 10, 5, 10);
            gc.ipady = 50;
            gc.gridwidth = GridBagConstraints.REMAINDER;
            gc.fill = GridBagConstraints.BOTH;
            add(j1, gc);
            add(j2, gc);
            add(j3, gc);
            add(j4, gc);
        }

    }
}

Have a look at How to Use GridBagLayout for more details