Layout Managers

hundredcarriageSoftware and s/w Development

Nov 3, 2013 (3 years and 10 months ago)

106 views


Page
1

of
3

SOFTWARE ENGINEERING

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.

It
is a modified version of the text

and images

from

the Sun Java Tutorial, which is accessible

online at:


http://ja
va.sun.com/docs/books/tutorial


FlowLayout




FlowLayout

is the default layout manager for every
JPanel
. It simply lays out components in a single
row, starting a new row if its container is
n

t sufficiently wide.
FlowLayout

sizes components

at their
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
using a
FlowLayout

constructor that takes an alignment argument. You can also specify how much
vertical or horizontal padding is put around the components.

The
FlowLayout

constructors are:


public FlowLayout()

public FlowLayout(int
alignment
)

public FlowLayout(int
alignment
,


int
horizontalGap
, int
verticalGap
)


The
alignment

argument can be
FlowLayout.LEADING
,
FlowLayout.CENTER
, or
FlowLayout.TRAILING
. When the
FlowLayout

controls a container with a left
-
to
-
right co
mponent
orientation (the default),
LEADING

specifies that the components be left
-
aligned and
TRAILING

specifies
right alignment. The
horizontalGap

and
verticalGap

arguments specify the number of pixels to put
between components. If you don

t specify a gap
value,
FlowLayout

uses
5

for the default gap value.



BoxLayout


The
BoxLayout

class puts components in a single row or column. It
respects the components


requested maximum sizes
(if any)
and also
lets you align components.
BoxLayout

either stacks its c
omponents on
top of each other or places them in a row


your choice. You might
think of it as a full
-
featured version of
FlowLayout
.

The two
arguments to the
BoxLayout

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


Page
2

of
3

Each component that is added to a
BoxLayout

panel has to have its alignment also specified:

listScroller.setAlignmentX(

LEFT_ALIGNMENT

);

listPane.add(

listScroller

);

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
below
illu
strate alignment of
components that
have restricted maximum widths, using
left alignment, center alignment, and right
alignment:





I
f

the components do not have restricted maximum width, and

all the
components have identical X alignment, then all com
ponents are
made
as wide as their container, as seen to the right.



BorderLayout




A
BorderLayout

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;
a
ll
remaining
extra space is placed in the
center area.

These areas are specified by the
BorderLayout

constants
PAGE_START
,
PAGE_END
,
LINE_START
,
LINE_END
, and
CENTER
. 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
BorderLayout

panel, you specify which of the areas

it will be

in
:



pane.add(button, BorderLayout.CENTER);



Page
3

of
3

CardLayout





The
CardLayout

class helps you manage two or more components (usually
JPanel

instances) that share
the same display space.

Conceptually, each component a
CardLayout

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
e container.



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
CardLayout

manages, you must specify a string that
ident
ifies the component being added:


cards =
new JPanel(new CardLayout());

cards.add(card1, "JPanel with JButtons");


Y
ou can use the
CardLayout

show
()

method to set the currently showing component. The first
argument to the
show
()

method is the container the
CardLayout

controls


that is, the contai
ner of the
components the
CardLayout

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.


SpringLayout


SpringLayout

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
be
dynamically calculated) from the right edge of a second component.


A common use of
SpringLayout

is to create a form like this one, with a
grid of attribute names and text entry fields to write values in.