You are on page 1of 6

GridBagLayout :

This is the most complex and flexible Layout of Java. It gives more control to add components into container according to our specifications. When we implement GridBagLayout to a Container. The attributes of the component, that will be added to the container are set by using GridBagConstraints. Various attributes available in

GridBagConstraints :
1. anchor 2. fill

3. 4. 5. 6. 7. 8. 9.

gridwidth gridheight gridx gridy weightx weighty insets

1. anchor : This attribute is used when control size is smaller than the area allotted to the control. By using this attribute we can set the alignment of the control within the allotted area. Example:
GridBagConstraints gbc = new GridBagConstraints();

gbc.anchor= GridBagConstraints.CENTER; GridBagConstraints.NORTH; GridBagConstraints.SOUTH; GridBagConstraints.EAST; GridBagConstraints.WEST; GridBagConstraints.NORTHEAST;

GridBagConstraints.NORTHWEST; GridBagConstraints.SOUTHEAST; GridBagConstraints.SOUTHWEST;

2. fill : This attribute is used when control size is smaller than the area allotted to the control. By using this attribute we can set that how the control will fill the remaining area. Example:
gbc.fill= GridBagConstraints.NONE; GridBagConstraints.HORIZONTAL; GridBagConstraints.VERTICAL; GridBagConstraints.BOTH;

3. gridwidth : This attribute is used to specify the No. of columns occupied by the Control. By default its value is 1. Example:
gbc.gridwidth=2;

4. gridheight : This attribute is used to specify the No. of Rows occupied by the Control. By default its value is 1. Example:
gbc.gridheight=2;

5. gridx : This attribute is used to specify the column position of the Control. By default its value is 0. Example:
gbc.gridx=2;

6. gridy : This attribute is used to specify the Row position of the Control. By default its value is 0. Example:
gbc.gridy=3;

7. weightx : This attribute is used to specify that how the control will fill the remaining horizontal space in the container. By default its value is 0.

Example:
gbc.weightx=1;

8. weighty : This attribute is used to specify that how the control will fill the remaining vertical space in the container. By default its value is 0. Example:
gbc.weighty=1;

9. insets : This attribute is used to specify spaces between the outer boundaries of the controls. By using this control we can set the top , bottom, left and right space values. Example:
Insets ss=new Insets(5,5,5,5); (Top, Left, Bottom, Right) gbc.insets=ss;

You might also like