Box and BoxLayout

Box is a panel that comes with a BoxLayout as its default layout manager. BoxLayout allows the developer to lay out a single row or a single column of components within a container. There are several ways to create and attach a BoxLayout object -
   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]


Example - BoxLayout

The second panel in the example interleaves three JButtons and four glue components. The glue objects take all of the extra space and divide it equally between themselves. As a result, this arrangement produces three evenly-spaced buttons. If there were one glue component to the left of the button labeled "L" and the rest of the glue were removed, all of the free space would be taken up by the glue on the left of the container, so the buttons would all be packed tightly together on the right. On the other hand, placing glue to the left of the left button and to the right of the right button would distribute the spare space equally on each side, leaving the three buttons huddled together in the center of the container. [Topley, p155]


Example - GridBagLayout

Instead of laying out lots of underlying cells, and then going to great pains to map each component to just the right set of adjacent cells, the example takes a very simple-minded approach: column 1 should be 3 times as wide as columns 2, 3, or 4. This is accomplished by setting weightx to 3 for column 1, and to 1 for the other columns. Even though weightx is supposed to be used for allocating space when resize events happen, it would seem as though it can also be used to establish initial relative sizes.

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.