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.
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.
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.
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 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));