Box horizontalFill = new Box( BoxLayout.X_AXIS ); Box verticalFill = Box.createVerticalBox(); JPanel aThird = new JPanel(); // note that the layout object is passed to the container // AND, that the container object is passed to the layout aThird.setLayout( new BoxLayout( aThird, BoxLayout.Y_AXIS ) );By default, BoxLayout positions its children starting at the top or left edge of the container. Any unused space ends up at the bottom or right end. If the container is (or becomes) too small for its children, they are clipped, not compressed. In order to distribute any empty space in more interesting ways, three special types of invisible components are provided: rigid areas, struts, and glue.
Rigid areas. A rigid area is a fixed-size, two-dimensional component. Adding one to a horizontal container inserts a horizontal gap equal to its width and ensures that the container is at least as tall as its height. [Topley, p152] They are created by calling a static creation method and added to a container exactly like other components -
p.setLayout( new BoxLayout( p, BoxLayout.X_AXIS ) ); p.add( Box.createRigidArea( new Dimension(8,40) ) );Struts. A strut is very similar to a rigid area, except that it is one-dimensional. A horizontal strut inserts a fixed-size horizontal gap in a horizontal layout, or, it enforces a minimum container width in a vertical layout.
p.setLayout( new BoxLayout( p, BoxLayout.X_AXIS ) ); p.add( Box.createHorizontalStrut( 16 ) );Glue. The name is perhaps a misnomer because it suggests something sticky. A glue component expands to take up as much space as it can. Horizontal glue occupies as much horizontal space as it can. Glue created using the createGlue() method expands in both directions.
p.setLayout( new BoxLayout( p, BoxLayout.X_AXIS ) ); p.add( Box.createGlue(); p.add( new JButton("L") ); p.add( Box.createHorizontalGlue() );[Reference: Topley, pp151-155]
Apparently, the rigid areas in the eighth panel can cause that panel's width to be greater, without messing up the width proportions of the other columns.
gridx and gridy are used to position panels 1 through 4 in column 1, and panels 5 through 8 each in their own column.
Panels 5 through 8 do not attempt to relate their height to the number or height of the panels in column 1. Instead, gridHeight has been set to GridBagConstraints.REMAINDER so that more panels could be added to column 1, and panels 5 through 8 would automatically adjust themselves.