Java AWT GridBagLayout Manager



Using Anchor Constraint Only

In the following program, when a button is clicked, the center button is anchored (change its position within the cell) accordingly. The center button does not occupy the complete cell. As it does not occupy the complete space of the cell, it can move around in the cell, as per the action given.

import java.awt.*;   
import java.awt.event.*;
public class UsingAnchorConstraint extends Frame  implements ActionListener  
{
  Button n, nw, w, ne, e, s, se, sw, cb ;
  GridBagLayout gbl ;  GridBagConstraints gbc ;

  public UsingAnchorConstraint()
  {
    gbl= new GridBagLayout();
    gbc = new GridBagConstraints();
    setLayout(gbl);
    setBackground(Color.cyan);
    gbc.insets = new Insets(5,5,5,5);
                                         // add the center button at the beginning as its constraints are very different from the rest
    cb = new Button("C");
    gbc.gridx = 1;    
    gbc.gridy = 1;
    gbc.fill = GridBagConstraints.NONE;    
    gbl.setConstraints(cb, gbc);
    add(cb);
    cb.addActionListener(this);
    cb.setBackground(Color.black); 
    cb.setForeground(Color.white);
                                    // add the first row first button
    nw = new Button("North West");
    gbc.gridx = 0;    
    gbc.gridy = 0;
    gbc.fill = GridBagConstraints.BOTH;
    gbc.weightx = 1.0; gbc.weighty = 1.0;
    gbl.setConstraints(nw, gbc);
    add(nw);  
    nw.addActionListener(this);
                                   // add the first row second button
    n = new Button("North");
    gbc.gridx = 1;   gbc.gridy = 0;
    gbc.weightx = 1.0;   gbc.weighty = 1.0;
    gbl.setConstraints(n, gbc);
    add(n) ;  
    n.addActionListener(this);
                                    // add the first row third button
    ne = new Button("North East");
    gbc.gridx = 2;  gbc.gridy = 0;    // other constraints are taken from the earlier settings
    gbl.setConstraints(ne, gbc);
    add(ne);  
    ne.addActionListener(this);
                                   // add the second row first button 
    w = new Button("West");
    gbc. gridx  = 0;    gbc.gridy  = 1;
    gbl.setConstraints(w, gbc);
    add(w);  
    w.addActionListener(this);
                                      // add the second row third button(second is added already at the beginning: center button)
    e = new Button("East");
    gbc.gridx = 2;   gbc.gridy = 1;
    gbl.setConstraints(e, gbc);
    add(e);  
    e.addActionListener(this);
                                   // add the third row first button
    sw = new Button("South West");
    gbc.gridx = 0;   gbc. gridy = 2;
    gbl.setConstraints(sw, gbc);
    add(sw);  
    sw.addActionListener(this);
                                   // add the third row second button
    s = new Button("South");
    gbc.gridx = 1;   gbc.gridy = 2;
    gbl.setConstraints(s, gbc);
    add(s);  
    s.addActionListener(this);
                                    // add the third row third button
    se = new Button("South East");
    gbc.gridx = 2;  gbc.gridy = 2;
    gbl.setConstraints(se, gbc);
    add(se);  
    se.addActionListener(this);
  }                                           // constructor closing
  public void actionPerformed(ActionEvent e)   
  {
                   // click a border button and the center button is positioned accordingly
    String str = e.getActionCommand() ;
    if(str.equals("North West"))           
        gbc.anchor = GridBagConstraints.NORTHWEST;
    else if(str.equals("North"))            
        gbc.anchor = GridBagConstraints.NORTH;                              
    else if(str.equals("West"))             
        gbc.anchor = GridBagConstraints.WEST;
    else if(str.equals("North East"))    
        gbc.anchor = GridBagConstraints.NORTHEAST;
    else if(str.equals("C"))      // clicking the center buttons also
        gbc.anchor = GridBagConstraints.CENTER;
    else if(str.equals("East"))              
        gbc.anchor = GridBagConstraints.EAST;
    else if(str.equals("South West"))   
        gbc.anchor = GridBagConstraints.SOUTHWEST;
    else if(str.equals("South"))            
        gbc.anchor = GridBagConstraints.SOUTH ;
    else if(str.equals("North East"))     
        gbc.anchor = GridBagConstraints.NORTHEAST;
    else if(str.equals("South East"))    
        gbc.anchor = GridBagConstraints.SOUTHEAST;

                         // the constraints of the center button are re-defined (in the following 
                         // code) as their values are changed due to the other buttons clicks.  

    gbc.gridx = 1;  gbc.gridy = 1;
    gbc.fill = GridBagConstraints.NONE;
    gbl.setConstraints(cb, gbc);
      
    invalidate(); 
    validate();                                  
  }  
  public static void main(String args[])
  {                                                                                          
    Frame f = new UsingAnchorConstraint();                                                      
    f.setTitle("Using Anchor Constraint");
    f.setSize(300,300);  
    f.setVisible(true);
  }
}
Output screen of UsingAnchorConstraint.java

gbc.insets = new Insets(5, 5, 5, 5);

The insets variable of GridBagConstraints class gives the space between the components and the border of the frame (not in between the components). The parameter order is top, left, bottom and right (anti-clockwise). All are given 5 pixels only.

gbc.fill = GridBagConstraints.NONE;

The above fill variable dictates the component should not grow to the full size of the cell.

gbc.fill = GridBagConstraints.BOTH;

The above fill variable dictates the component should grow to the full size of the cell both horizontally and vertically. That is, the component occupies the whole cell. In the above program, except the center component, each component occupies complete cell.

gbc.weightx = 1.0; gbc.weighty = 1.0;

weightx and weighty fields control the space distribution among cells when the frame is resized so that the frame is larger than the actual space required for the cells. The maximum value is 1.0 and means it gives more weight (preference) to the cell to grow. All the components are given 1.0 except the center one, “C“. The center one takes a default value of 0 and means does not grow when the frame is resized.

gbc.anchor = GridBagConstraints.NORTHWEST;

When a button is clicked, the center button is repositioned within its cell. The above statement places the center button in the North West corner of the cell. Like this, the position can be 9 ways – 4 sides, 4 corners and center.

invalidate();
alidate();

Whenever a layout is disturbed either by adding a new component or deleting an existing component, these methods are to be called by the programmer. The invalidate() method invalidates the existing layout setup and validate() method validates the components with the new setup.

Leave a Comment

Your email address will not be published.