A graphical user interface uses graphical components (e.g.,
JLabel) to enable a user to interact
with an application. These components need to be placed into a container
JPanel) to build an interface.
The way the components are grouped together in the container are very
important in making sure the interface is useful to the user. If you've
ever used a program where it wasn't very clear how to perform a function
you'll know it can be a frustrating experience. Where you place the
components in a container can be made easier by choosing the right layout
A layout manager specifies the position and size of the components inside a container. There are several layout managers provided in the Java API.
JPanel uses the
FlowLayout as its default
layout manger. It's the simplest layout manager as it places the
components in a row. If the row reaches the edge of the container it
starts another row and so on.
Have a look at Using the FlowLayout for more specific details on how to
If you run the GoingWithTheFlow program you can see the
in action. Every time you press the "Add Button" button a new button
appears on the row. If you keep adding buttons the
will be filled with row after row of buttons. The FlowLayoutExample
program provides a user with the ability to set alignment and component
spacing options for a
FlowLayout manager of a
BorderLayout splits the container into five different
sections. The top and bottom sections are called north and south. They run
from the left edge of the container to the right edge. The space in
between the top and bottom sections is split into three sections known as
west, center and east. If you run the BorderLayoutExample
program you'll see how the sections appear on a
Have a look at Using the BorderLayout for more specific
details on how to use the
BoxLayout puts the components either in one row or
column. Unlike the
FlowLayout there is no wrapping around to
another row or column if the edge of the container is reached. If there
are too many components to fit in the row then the ones that don't fit
can't be seen.
The GridLayout places each component into a cell of a grid made up of a specified number of rows and columns (e.g., think of a spreadsheet). Each cell in the grid is exactly the same size and the component is sized to take up the entire cell size.
The GridLayout example program shows how a
GridLayout can be used to make a simple calculator graphical user interface. It also allows the user to customize the
GridBagLayout is similar to the
in that it uses a grid made up of a number of rows and columns. The
difference is that the rows and columns do not have to be the same height
or width. Also, a component can span across a number of cells.
GroupLayout can be quite complicated to use as it deals
with the horizontal and vertical layouts separately. Therefore each
component must be specified in both the horizontal and vertical layouts.
CardLayout allows you to make an area which can display
different components at different times. Like a stack of playing cards
only the top card is seen.
SpringLayout is one of the most flexible layout
managers as the position of each component is defined by the distance to
the edges of another component. It can emulate most of the other layout
managers but the more components you work with the more complex the layout