GridBagLayout
Peter van der Linden in Just Java and Beyond 3rd Edition describes the GridBagLayout manager as "excessively complicated " and doesn't recommend it. Core Java merely says "using grid bag layouts can be incredibly complex". Whilst it is complex to use by hand, the various GUI tools such as VisualCafe, Visual Age, JBuilder etc etc make it easier to use, if not understand. Thus JBuilder will happily modify the add statement to include the following details for the GridBagLayout class.add(pAps,new GridBagConstraints2(1, GridBagConstraints.RELATIVE, 
GridBagConstraints.RELATIVE, 3, 0.0, 0.0,GridBagConstraints.CENTER, 
GridBagConstraints.NONE, new Insets(0, 0, 0, 0), -3, 45));But when you create your code by hand it does not need to look as complex as this.Feedback from people who have taken the exam indicates that the questions on the GridBagLayout are not very in-depth and a basic understanding of the various fields of the GridBagConstraints class may well be adequate.My favorite Java Tool is Borland/Inprise JBuilder which has its own Layout Manager called the XYLayout manager. This seems to be easier to use than the GridBagLayout, but if you are writing for the net it would require users to download that additional class, causing additional overhead.GridBagLayout is a little like the GridLayout except that different cell rows can have different heights, and columns can have different widths. The Java2 Docs come with a demonstration applet that shows what can be done with the GridBagLayout manager.In the exam this is a prime moment to take advantage of the scrap paper to write out a grid and consider the effect of each cell. One of the problems with the GridBagLayout is that instead of being based strictly on the underlying grid, Java tries to guess the cells from the information given. The GridBagLayout manager uses a helper class GridBagConstraints which has a set of member variables that can be set to affect the appearance of each component. The fields that you modify on the GridBagConstraints class act as "suggestions" as to where the components will go. An instance of GridBagConstraints is passed as a parameter with the add method, in the form add(component, GridBagConstraint); 
GridBagConstraints goes against the general convention in Java in that you might expect its attributes to be configured withsetFooParam() 
methods, where FooParam might be WeightX/Y or Padding between components. 
Instead it takes the form
GridBagLayout gbl=new GridBagLayout();
gbl.weightx=100;
If you use the GridBagLayout without the GridBagConstraints class it acts a little like a FlowLayout, simply dropping the components onto the background one by one.I have created a simple demonstration applet with source that shows how nothing much happens unless you play with the GridBagConstraints class..http://www.software.u-net.com/Applets/GridBagDemo/GridBagTest.htm The GridBagLayout acts a little more like the GridLayout if you use the GridBagConstraints class and use the gridx and gridy fields to assign a position in a "virtual" grid to each component as you add it. This applet demonstrates this possibility. This is still a little dull and very like the other layout managers. Things start to get much more interesting when you start to modify other fields of the GridBagConstraints class to modify the appearance of different components within this "virtual" grid.
Remember that although you need to understand this for the purposes of the exam, it might be easier when programming in the real world to use a combination of container controls added with other layout managers. An example of when this is not an option is when you need to dynamically re-size components. This is a situation where GUI builders such as Visual Cafe or JBuilder are not much help and an understanding of the GridBagLayout may be essential.I have created a demonstration applet that shows the affect of dynamically changing the padding parameters for a single button in a group of buttons set out with a GridbagLayout managerThe fields for the GridBagConstraints class aregridx gridy 
gridwidth and gridheight 
fill 
ipadx and ipady 
insets 
anchor 
weightx and weighty 
Whilst browsing Bill Brogdens excellent Java2 Exam Cram Book I found a pointer to a comprehensive demo of the GridBagLayout athttp://www.austria.eu.net/java/programs/applets/missgable/index.htm Using gridx and gridy to suggest component placing
For this example you are doing some basic code to design a appointment calendar program. It will show times down the left hand side and appointment details down the right. The time units will be in half hour chunks.Because an appointment may cover more than one time unit, ie may last an hour and a half you need to be able to dynamically change the height of an appointment to cover more than one half hour time unit. Because of this requirement to have a varying height for the appointments, a GridLayout is not suitable. You will be placing panels on the Frame as containers. The first step is to ensure that each panel sits side by side on the main Frame of the Application. import java.awt.*;import java.awt.event.*;public class GBCal extends Frame{    Panel pTimes=new Panel();    Panel pAps=new Panel();    TextField txTimes=new TextField("09.00");    TextField txAps=new TextField("Meet the boss");    GridBagLayout gbl=new GridBagLayout();    GridBagConstraints gbc=new GridBagConstraints();    public static void main(String argv[]){    GBCal gbc=new GBCal();    gbc.setLayout(new FlowLayout());}public GBCal() {    pTimes.add(txTimes);    pAps.add(txAps);    setLayout(gbl);    gbc.gridx=0;    gbc.gridy=0;    pTimes.setBackground(Color.pink);    add(pTimes,gbc);    gbc.gridx=1;    gbc.gridy=0;    pAps.setBackground(Color.lightGray);    add(pAps,gbc);    setSize(300,300);    setVisible(true);    }}The output will appear as followsNote how the GridBagLayout and the GridBagConstraints classes work together. The GridBagConstraints instance gbc gets re-used for each time a component is added. At no point do you specifically state the number of rows and columns for the Grid as the GridBagLayout class deduces it from the gridx and gridy fields of the GridBagConstraints instance.ipadx and ipady to control the internal padding of components The code has set the background color so you can see the extent of the panel rather than simply the width of the text fields. This is fine but now you want the fields to stretch all the way from left to right of the main application Frame. This can be performed by modifying the ipadx field of the GridBagConstraints class. This is peformed by settinggbc.ipadx=30;
For the times and
gbc.ipadx=100;
For the appointments
The result is as followsComponents within a panel with the GridBaglayout
For the next step I want to give each panel its own GridBagLayout manager and add additional time slots and appointments. For the purpose of this example I will add just one more time slot and simply stretch the single appointment to cover the time slots between 9.00 and 9.30.To do this I will create a new instance of GridBagLayout called gbBut and use it to set up the grid for the pTimes panel to place the time slot fields one on top of the other vertically.The code that performs this is
//Control the Times panel with a GridBagLayoutpTimes.setLayout(gbBut);gbc.gridx=0;gbc.gridy=0;pTimes.add(txTimes9,gbc);gbc.gridx=0;gbc.gridy=1;pTimes.add(txTimes930,gbc);Here is the complete code to the revised programimport java.awt.*;import java.awt.event.*;public class GBCal extends Frame{Panel pTimes=new Panel();Panel pAps=new Panel();TextField txTimes9=new TextField("09.00");TextField txTimes930=new TextField("09.30");TextField txAps=new TextField("Meet the boss");GridBagLayout gbl=new GridBagLayout();GridBagLayout gbBut=new GridBagLayout();GridBagConstraints gbc=new GridBagConstraints();public static void main(String argv[]){    GBCal gbc=new GBCal();    gbc.setLayout(new FlowLayout());    }public GBCal() {    setLayout(gbl);    //Control the Times panel with     //a GridBagLayout    pTimes.setLayout(gbBut);    gbc.gridx=0;    gbc.gridy=0;    pTimes.add(txTimes9,gbc);    gbc.gridx=0;    gbc.gridy=1;    pTimes.add(txTimes930,gbc);    pTimes.setBackground(Color.pink);    //Re-using gbc for the main panel layout     gbc.gridx=0;    gbc.gridy=0;     gbc.ipadx=30;    add(pTimes,gbc);    pAps.setLayout(gbBut);    gbc.gridx=0;    gbc.gridy=1;    pAps.add(txAps,gbc);    gbc.gridx=1;    gbc.gridy=0;    gbc.ipadx=100;    pAps.setBackground(Color.lightGray);    add(pAps,gbc);    setSize(300,300);    setVisible(true);    }}//End of classThe resulting output is as followedThis has worked up to a point. We now have the two time slots, but unfortunatly the one appointment has defaulted to the centre of the appointments field and it is only one row thick. What I want is for it to be anchored at the top of the appointments area and to stretch to cover both time slots.Anchoring components within the grid
f a component does not fill the whole area, you can specify where in the area you want it using the anchor field of the GridBagConstraints class. The possible values are
GridBagconstraints.CENTERGridBagconstraints.NORTHGridBagconstraints.NORTHEAST
etc etcIn this case I want to position the fields at the top (North) of the containing panels. I have increased the depth of the Appointment field by increasing the ipady value for the address field.Here is the code to do this.import java.awt.*;import java.awt.event.*;public class GBCal extends Frame{Panel pTimes=new Panel();Panel pAps=new Panel();TextField txTimes9=new TextField("09.00");TextField txTimes930=new TextField("09.30");TextField txAps=new TextField("Meet the boss");GridBagLayout gbl=new GridBagLayout();GridBagLayout gbBut=new GridBagLayout();GridBagConstraints gbc=new GridBagConstraints();public static void main(String argv[]){        GBCal gbc=new GBCal();        gbc.setLayout(new FlowLayout());    }public GBCal() {    setLayout(gbl);    //Control the Times panel with a GridBagLayout    pTimes.setLayout(gbBut);    //Ensure the componants sit at    //the top of the containers    gbc.anchor=GridBagConstraints.NORTH;    gbc.gridx=0;    gbc.gridy=0;    pTimes.add(txTimes9,gbc);    gbc.gridx=0;    gbc.gridy=1;    pTimes.add(txTimes930,gbc);    pTimes.setBackground(Color.pink);    //Re-using gbc for the main panel layout     gbc.gridx=0;    gbc.gridy=0;     gbc.ipadx=30;    add(pTimes,gbc);    pAps.setLayout(gbBut);    gbc.gridx=0;    gbc.gridy=1;    gbc.ipady=12;    pAps.add(txAps,gbc);    gbc.gridx=1;    gbc.gridy=0;    gbc.ipadx=100;    pAps.setBackground(Color.lightGray);    add(pAps,gbc);    setSize(300,300);    setVisible(true);    }}//End of class
The output of this code is as followsGridBag items not covered by this exercise
This exercise has covered the following GridBagConstraints fieldsipadx/y 
gridx/y 
anchor 
The GridBagConstraints class has the following important fieldsweightx/y 
fill 
gridwidth/height 
insets The weight fields control how an area grows or shrinks beyond its initial size. So if you set the weighty field to zero the field will remain a constant height when you resize the window.The fill field controls how a component stretches to fill the area. Like the anchor field you set the fill values using constants of the GridBagConstraints class. These are
GridBagConstraints.NONEGridBagConstraints.HORIZONTALGridBagConstraints.VERTICALGridBagConstraints.BOTHThe gridwidth/height fields determine how many columns and rows a component occupies.The insets field indicates the "external" padding along the cell boundaries. 
--------------------------------------------------------------------------------