Swing Layout Managers
This handout briefly overviews some of the Swing Layout Managers and how they can be used to
arrange the components within a container in your GUI.
is a modified version of the text
the Sun Java Tutorial, which is accessible
is the default layout manager for every
. It simply lays out components in a single
row, starting a new row if its container is
t sufficiently wide.
preferred size. If the container is wider than necessary for a row of components, the row is, by default,
centered horizontally within the container. You can specify that it stick to the left or
right side instead by
constructor that takes an alignment argument. You can also specify how much
vertical or horizontal padding is put around the components.
argument can be
. When the
controls a container with a left
orientation (the default),
specifies that the components be left
right alignment. The
arguments specify the number of pixels to put
between components. If you don
t specify a gap
for the default gap value.
class puts components in a single row or column. It
respects the components
requested maximum sizes
lets you align components.
either stacks its c
top of each other or places them in a row
your choice. You might
think of it as a full
featured version of
arguments to the
constructor are the container that it
manages and the axis along which the components will be laid out.
pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));
Each component that is added to a
panel has to have its alignment also specified:
The alignments affect not only the components
positions relative to each other, but also the location of
the components (as a group) within their container. The figures
strate alignment of
have restricted maximum widths, using
left alignment, center alignment, and right
the components do not have restricted maximum width, and
components have identical X alignment, then all com
as wide as their container, as seen to the right.
places components in up to five areas: top, bottom, left, right, and center.
Each area is
as large as it needs to be to fit the components placed into it;
extra space is placed in the
These areas are specified by the
. If you enlarge the window, the center area gets as much of the
available space as possible. The other areas expand only as much as necessary to fill all available space.
When you add a component to a
panel, you specify which of the areas
it will be
class helps you manage two or more components (usually
instances) that share
the same display space.
Conceptually, each component a
manages is like a playing card or
trading card in a stack, where only the top card is visible at any time. You can choose
which card to
show on top
in any of the following ways:
By asking for either the first or last card, in the order it was added to th
By flipping through the deck backwards or forwards.
By specifying a card with a specific name.
When you add a component to a container that a
manages, you must specify a string that
ifies the component being added:
new JPanel(new CardLayout());
cards.add(card1, "JPanel with JButtons");
ou can use the
method to set the currently showing component. The first
argument to the
method is the container the
that is, the contai
ner of the
manages. The second argument is the string that identifies the component
to show. This string is the same as was used when adding the component to the container.
is a flexible layout manager designed for use by GUI
builders. It lets you specify precise relationships between the edges of
components under its control. For example, you might define that the
left edge of one component is a certain distance (which can
dynamically calculated) from the right edge of a second component.
A common use of
is to create a form like this one, with a
grid of attribute names and text entry fields to write values in.