# Java Notes

# BoxLayout spacing

## Fillers - rigid areas, glue, struts, and custom Fillers

Invisible components can be added to a layout to produce empty space between components. The most useful are rigid areas (or struts). Glue may be insert expandable empty space in a layout.

- Rigid areas have a fixed horizontal and vertical size.
- Struts are fixed either vertically or horizontally. Generally use rigid areas instead of struts.
- Glue is an expandable component. Used if you want spacing to increase when a window is resized.
- Box.Filler is a more general spacing component with specified minimum, preferred, and maximum sizes.

## Rigid Area

A rigid area is very is a fixed size filler with vertical and horizontal dimensions. It can be used in either horizontal or vertical layouts. Create a rigid area by specifying its width and height in pixels in a Dimension object, which has two parameters, an x distance and a y distance. Typically the dimension you are not interested in is set to zero.

    p.add(Box.createRigidArea(new Dimension(10, 0)));

This creates a rigid area component 10 pixels wide and 0 pixels high and adds it to a panel.

## Glue

       p.add(Box.createVerticalGlue());    // expandable vertical space.
       p.add(Box.createHorizontalGlue());  // expandable horizontal space.

Glue is an invisible component that can expand. It's more like a spring or sponge than glue. Put a glue component where extra space should appear (disappear from) when a window is resized. Use vertical glue in a vertical BoxLayout and horizontal glue in a horizontal layout. For example, this will allow extra vertical space between two buttons.

    JPanel p = new JPanel();
    p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
    p.add(button1);
    p.add(Box.createVerticalGlue()); // This will expand/contract as needed.
    p.add(button2);

Use `Box.createHorizontalGlue()` for horizontal expansion.

## Struts

       p.add(Box.createVerticalStrut(n));   // n pixels of vertical space.
       p.add(Box.createHorizontalStrut(n)); // n pixels of horizontal space.

Create a strut by specifying its size in pixels, and adding it to the panel at the point you want the space between other components. Important: Use horizontal struts only in horizontal layouts and vertical struts only in vertical layouts, otherwise there will be problems. To avoid problems from nested panels, you are generally better off using a rigid area.

    p.add(Box.createHorizontalStrut(10));

This creates a strut component 10 pixels wide and adds it to a panel.

## Box.Filler

You can create your own filler by using the Box.Filler constructor and specifying the minimum, preferred, and maximum size. Each size must be in a Dimension object, which has width and height.

    Box.Filler myFiller = new Box.Filler(min, pref, max);

For example, to create a new horizontal filler that can get no smaller that 4 pixels, that prefers to be 16 pixels wide, and that will expand to no more than 32 pixels, you could do this:

    Box.Filler hFill = new Box.Filler(new Dimension(4,0),
                                     new Dimension(16, 0),
                                     new Dimension(32, 0));