Laying Out Components Within a Container |
Every container, by default, has a layout manager -- an object that implements theLayoutManager
interface.* If a container's default layout manager doesn't suit your needs, you can easily replace it with another one. The Java platform supplies layout managers that range from the very simple (FlowLayout
andGridLayout
) to the special purpose (BorderLayout
andCardLayout
) to the very flexible (GridBagLayout
andBoxLayout
).This section gives you an overview of the layout managers the Java platform provides, gives you some general rules for using layout managers, and then tells you how to use each of the provided layout managers. It also points to examples of using each layout manager.
General Rules for Using Layout Managers
This section answers some common questions about layout managers:
- How do you choose a layout manager?
- How do you create a layout manager, associate it with a container, and tell it to start working?
- How does the layout manager know what components it manages?
How to Use BorderLayout
BorderLayout
is the default layout manager for every content pane (an important container found in all frames, applets, and dialogs). ABorderLayout
uses five areas to hold components: north, south, east, west, and center. All extra space is placed in the center area. Here's an applet that puts one button in each area:
This is a picture of the applet's GUI. To run the applet, click the picture. The applet will appear in a new browser window.How to Use BoxLayout
TheBoxLayout
class puts components in a single row or column. It respects the components' requested maximum sizes, and also lets you align components. Here's an applet that uses aBoxLayout
to put a bunch of buttons in a centered column:
This is a picture of the applet's GUI. To run the applet, click the picture. The applet will appear in a new browser window.How to Use CardLayout
TheCardLayout
class lets you implement an area that contains different components at different times. Tabbed panes are intermediate Swing containers that provide similar functionality, but with a pre-defined GUI. ACardLayout
is often controlled by a combo box , with the state of the combo box determining which panel (group of components) theCardLayout
displays. Here's an applet that uses a combo box andCardLayout
in this way:
This is a picture of the applet's GUI. To run the applet, click the picture. The applet will appear in a new browser window.How to Use FlowLayout
FlowLayout
is the default layout manager for everyJPanel
. It simply lays out components from left to right, starting new rows if necessary. Both panels in theCardLayout
applet above useFlowLayout
. Here's another example of an applet that uses aFlowLayout
:
This is a picture of the applet's GUI. To run the applet, click the picture. The applet will appear in a new browser window.How to Use GridLayout
GridLayout
simply makes a bunch of components equal in size and displays them in the requested number of rows and columns. Here's an applet that uses aGridLayout
to control the display of five buttons:
This is a picture of the applet's GUI. To run the applet, click the picture. The applet will appear in a new browser window.How to Use GridBagLayout
GridBagLayout
is the most sophisticated, flexible layout manager the Java platform provides. It aligns components by placing them within a grid of cells, allowing some components to span more than one cell. The rows in the grid aren't necessarily all the same height; similarly, grid columns can have different widths. Here's an applet that uses aGridBagLayout
to manage ten buttons in a panel:
This is a picture of the applet's GUI. To run the applet, click the picture. The applet will appear in a new browser window.
LayoutManager2
, was introduced.
LayoutManager2
extends
LayoutManager
,
providing support for maximum size and alignment.
Currently, only BoxLayout
implements
LayoutManager2
.
All the other layout managers in the Java platform implement only
LayoutManager
.
Laying Out Components Within a Container |