home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


Book Home Java Enterprise in a Nutshell Search this book

2.5. Layout Management

Some containers, such as JTabbedPane and JSplitPane, define a particular arrangement for their children. Other containers such as JPanel (and JFrame, JDialog, and other top-level containers that use JPanel as their default content pane) do not define any particular arrangement. When working with containers of this type, you must specify a LayoutManager object to arrange the children within the container.

AWT and Swing include various implementations of the java.awt.LayoutManager interface. Each arranges components in a different way. Table 2-5 lists the layout managers defined by AWT. Swing applications often rely on these AWT layout managers, but Swing also defines some of its own, which are listed in Table 2-6. Figure 2-2 shows how some of these layout managers arrange their children.

Table 2-5. AWT Layout Managers

Layout Manager Description
BorderLayout

Lays out a maximum of five components: one along each of the four borders of the container and one in the center. When using this layout manager, you must add components to the container using a two-argument version of the add() method. The constraint argument should be one of the strings "North", "East", "South", "West", or "Center". Despite the simplicity of this layout system, this layout manager is used quite often.

CardLayout

Makes each component as large as the container and displays only one at a time. Various methods change the currently displayed component.

FlowLayout

Arranges components like words on a page: from left to right in rows and then top to bottom as each row fills up. Rows may be left, center, or right justified.

GridBagLayout

A flexible layout manager that arranges components in a grid with variable-sized cells. Allows explicit control over the way each component is resized when the container changes size. Requires a complex constraints set specified with the GridBagConstraints object.

GridLayout

Makes all components the same size and arranges them in a grid of specified dimensions.

figure

Figure 2-2. Layout managers

Table 2-6. Swing Layout Managers

Layout Manager Description
BoxLayout

The layout manager used by the Box container. It arranges its children into either a row or a column. It uses the glue and strut components returned by static Box methods to display stretchy and rigid spaces between the children.

OverlayLayout

An obscure and infrequently used layout manager that overlaps its children based on the children's alignment values specified with the setAlignmentX() and setAlignmentY() methods inherited from JComponent. Used by AbstractButton.

ScrollPaneLayout

A specialized layout manager used by JScrollPane. Not typically useful for general-purpose layouts.

ViewportLayout

A specialized layout manager used by JViewport. Not useful for general-purpose layouts.

Some layout managers require additional information about the components they are to arrange. This information takes the form of a constraint string or constraint object passed to the add() method when the component is added to its container. java.awt.BorderLayout is the most commonly used of these layout managers: its constraint object is a string that specifies where the child should be positioned within the container. Example 2-1 showed a typical use of BorderLayout.

Every AWT and Swing container has a default layout manager. If you explicitly set the layout manager to null, however, you can arrange your components using hardcoded sizes and positions. Set the size and position with methods such as setSize() and setLocation(). However, hardcoding the layout of your components makes your GUI less portable, harder to customize, and harder to translate into other languages.



Library Navigation Links

Copyright © 2001 O'Reilly & Associates. All rights reserved.