title: Understanding GridBagLayout constraints
body: <p>I am relatively new to swing and I am trying to figure out exactly how it works. Tell me if I am wrong.</p>
  
  <p>I have mostly figured out gridx and gridy which is necessary to place a component. You need at least n values of (gridx,gridy) to create an nxn grid. For example (5,5),(3,3),(4,9),(3,10) will create a 3x4 gridspace(4 rows, 3 columns) with the components using the above (gridx,gridy) respectively placed in cells (3,2),(1,2),(2,3),(1,4).</p>
  
  <p>weightx and weighty seems to have 2 functions, a >0 value of weightx and weighy stretches the grid cells to the edges(otherwise it's centralised) and we can also set proportions to size of the component - so if one component has gridx=0.1 and anothr has 0.2, then the later one is likely to be twice as wide. However, while proportioning components, the minimum default width and height of a component is respected.</p>
  
  <p>fill is required to stretch a component to the edges of the cells. Without fill, the component will remain at the center.</p>
  
  <p>but we can use anchor in such cases to position the component say along the north west corner of the cell instead of center.</p>
  
  <p>insets create a wall of space inside the edges of the cell.</p>
  
  <p>ipadx and ipady pushes the boundaries of the column or row containing the cell.</p>
  
  <p><strong>But</strong> I am not able to figure out very well how gridwidth and gridheight works.</p>
  
  <p>Consider this <strong>example below</strong>.Here 4 buttons are placed in cells b1(3,2),b2(1,1),b3(2,3),b4(4,4)
  of 4x4 gridspace.</p>
  
  <p><strong>How do I make the buttons , say b2 or b4, occupy the whole row or column of the cell that it is occupying?</strong></p>
  
  <pre><code>import javax.swing.*;
  import java.awt.*;
  //import java.awt.event.*;
  public class Swing29a
  {
  public static void main(String[] args)
  {
  JFrame f1= new JFrame("GridBag Layout Test");
  
  f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  f1.setResizable(true);
  f1.setLocation(200,100);
  f1.setSize(800,800);
  
  JPanel p1 = new JPanel();
  p1.setBackground(Color.black);
  f1.add(p1);
  
  
  JButton b1= new JButton("Button 1");
  b1.setBackground(Color.white);
  
  JButton b2= new JButton("Button 2");
  b2.setBackground(Color.white);
  
  JButton b3= new JButton("Button 3");
  b3.setBackground(Color.white);
  
  JButton b4= new JButton("Button 4");
  b4.setBackground(Color.white);
  
  
  
  GridBagLayout gm1= new GridBagLayout();
  p1.setLayout(gm1);
  GridBagConstraints cns =new GridBagConstraints();
  
  cns.gridx=5;
  cns.gridy=5;
  cns.weightx=0.1;
  cns.weighty=0.1;
  cns.fill=GridBagConstraints.BOTH;
  
  p1.add(b1,cns);
  
  cns.gridx=3;
  cns.gridy=3;
  p1.add(b2,cns);
  
  
  cns.gridx=4;
  cns.gridy=9;
  p1.add(b3,cns);
  
  cns.gridx=7;//3;
  cns.gridy=10;
  cns.gridheight=3;
  cns.weightx=0.2;
  cns.weighty=0.2;
  //cns.weightx=10.0;
  //cns.weighty=9.0;    
  //cns.ipadx=50;
  //cns.ipady=50;
  p1.add(b4,cns);
  
  f1.setVisible(true);
  
  }
  }
  </code></pre>
  
  <p><strong>EDIT:</strong>
  Here is a <strong>LINK TO IMAGE</strong>
  <a href="http://postimg.org/image/wae2x4w4z/" rel="nofollow">http://postimg.org/image/wae2x4w4z/</a></p>
  
  <p>I want any of the buttons to be able to fill the whole row or column, or take at least 2 cells in a row.</p>