GridBagLayout gbl = new GridBagLayout();
container.setLayout(gbl);
// Add components to container and gbl
// Force the layout of components before calling getLayoutDimensions()
gbl.layoutContainer(container);
// Get the dimensions
int[][] dim = gbl.getLayoutDimensions();
int cols = dim[0].length;
int rows = dim[1].length;