Java: BoxLayout spacing
Fillers - struts, glue, rigid areas, and custom Fillers
Invisible components can be added to a BoxLayout
to produce empty space between components. The most useful
are struts (or rigid areas). Glue may be insert empty space
to fill out a panel.
- Struts are non-expandable, fixed-size components, either vertical or horizontal.
- Rigid areas specify a fixed horizontal and vertical size.
- Glue is an expandable component. Used if you want spacing to increase when
a window is resized.
- Box.Filler is a class that lets
you create your own fillers with specified minimum, preferred, and maximum sizes.
Strut
p.add(Box.createVerticalStrut(n)); // n pixels of vertical space.
p.add(Box.createHorizontalStrut(n)); // n pixels of horizontal space.
When you want to specify space between components
use a strut. Create a strut by
specifying it's 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.
p.add(Box.createHorizontalStrut(10));
This creates a strut component 10 pixels wide and adds it to a panel.
Rigid Area
A rigid area is very similar to a strut, but has both
vertical and horizontal dimensions, consequently, a
rigid area can be used in either horizontal or vertical layouts,
altho often they are created with the unused size set to zero.
Create a rigid area by
specifying it's 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. This has the same effect
as the horizontal strut defined above.
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.
Two methods in the Box class create glue.
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
p.add(button2);
Use the Box.createHorizontalGlue()
method
for horizontal layouts.
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));