Java Reference
In-Depth Information
public static Component createGlue()
// Direction independent
Component glue = Box.createGlue();
aBox.add(glue);
public static Component createHorizontalGlue();
// Direction dependent: horizontal
Component horizontalGlue = Box.createHorizontalGlue();
aBox.add(horizontalGlue);
public static Component createVerticalGlue()
// Direction dependent: vertical
Component verticalGlue = Box.createVerticalGlue();
aBox.add(verticalGlue);
Once you create glue, you add it to a container in the same way as any other component,
by using
Container.add(Component)
or one of the other
add()
varieties. Glue allows you to align
components within a container, as Figure 11-2 shows.
Figure 11-2.
Using glue in a Box
You can add glue components to any container whose layout manager honors minimum,
maximum, and preferred size properties of a component, such as
BoxLayout
. For instance,
Figure 11-3 demonstrates what happens when you add a glue component to a
JMenuBar
just
before adding the last
JMenu
. Because the layout manager for a
JMenuBar
is
BoxLayout
(actually
the subclass
javax.swing.plaf.basic.DefaultMenuLayout
), this action pushes the last menu to
the right edge of the menu bar, similar to the Motif/CDE style of help menus.
■
Caution
I recommend that you avoid using the glue capability described here to set up help menus on
menu bars. Eventually, the
public void setHelpMenu(JMenu menu)
of
JMenuBar
will be implemented
and won't throw an
Error
. Of course, many of us are still waiting for this to happen.