GridBagLayout : fill and anchor

anchor : To specify the position of acomponet in a display area, when the component is smaller than the display area. The valid values of the data member anchor are provided in below:

GridBagConstraints.CENTER                      To place the component at the centre of the container.
GridBagConstraints.NORTH                       To place in the north part of the container.
GridBagConstraints.NORTHEAST           To place in the north-east part of the container.
GridBagConstraints.NORTHWEST           To place in the north-west part of the container.
GridBagConstraints.SOUTH                       To place in the south part of the container.
GridBagConstraints.SOUTHEAST           To place in the south-east part of the container.
GridBagConstraints.SOUTHWEST           To place in the south-west part of the container.
GridBagConstraints.EAST                           To place in the east part of the container.

The second type of values that can be given to anchor is relative, which means the values are relative to the container’s orientation, which might differ for non-Western languages. The relative values are shown here:

GridBagConstraints.FIRST_LINE_END ;
GridBagConstraints.LINE_END;
GridBagConstraints.FIRST_LINE_START;
GridBagConstraints.LINE_START;
GridBagConstraints.LAST_LINE_END;
GridBagConstraints.PAGE_END;
GridBagConstraints.LAST_LINE_START;
GridBagConstraints.PAGE_START;

Program

import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;

class GridBagLayt extends JFrame {
   
    GridBagLayt()
    {
        setTitle("GridBagLayout");
        GridBagLayout lay = new GridBagLayout();
        GridBagConstraints cons = new GridBagConstraints();
        setLayout(lay);
        setweightxAndweighty(cons,1,1);
        
        cons.fill = GridBagConstraints.BOTH;
        cons.anchor = GridBagConstraints.CENTER;
        setButton("button1",0,0,cons,lay);
        
        cons.fill = GridBagConstraints.VERTICAL;
        cons.anchor = GridBagConstraints.SOUTHWEST;
        setButton("button2",0,1,cons,lay);
        
        cons.fill = GridBagConstraints.HORIZONTAL;
        cons.anchor = GridBagConstraints.FIRST_LINE_START;
        setButton("button3",0,2,cons,lay);
        
        cons.fill = GridBagConstraints.VERTICAL;
        cons.anchor = GridBagConstraints.NORTHEAST;
        setButton("button4",1,0,cons,lay);
        
        cons.fill = GridBagConstraints.HORIZONTAL;
        cons.anchor = GridBagConstraints.CENTER;
        setButton("button5",1,1,cons,lay);
        
        cons.fill = GridBagConstraints.NONE;
        cons.anchor = GridBagConstraints.NORTH;
        setButton("button6",1,2,cons,lay);
        
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(700,200);
        setVisible(true);
    }
    
    void setButton(String name,int y,int x,GridBagConstraints gbc,GridBagLayout lay)
    {
        JButton jb = new JButton(name);
        gbc.gridy = y;
        gbc.gridx = x;
        lay.setConstraints(jb, gbc);
        add(jb);
    }
    
    void setweightxAndweighty(GridBagConstraints gbc,int weighty,int weightx)
    {
        gbc.weighty = weighty;
        gbc.weightx = weightx;
    }
}

public class Javaapp {
   
    public static void main(String[] args) {
       
        GridBagLayt gb = new GridBagLayt();
    }
}

Program Output

Leave a Comment