Previous | Next | Trail Map | Creating a User Interface (with Swing) | Laying Out Components within a Container

General Rules for Using Layout Managers

Unless you explicitly tell a container not to use a layout manager, it is associated with its very own instance of a layout manager. This layout manager is automatically consulted each time the container might need to change its appearance. Most layout managers don't require programs to directly call their methods.

How to Choose a Layout Manager

The layout managers provided by the AWT have different strengths and weaknesses. This section discusses some common layout scenarios and which AWT layout managers might work for each scenario. If none of the AWT layout managers is right for your situation, feel free to use other layout managers, such as the freely available PackerLayout

Scenario: You need to display a component in as much space as it can get.
Consider using BorderLayout or GridBagLayout. If you use BorderLayout, you'll need to put the space-hungry component in the center. With GridBagLayout, you'll need to set the constraints for the component so that fill=GridBagConstraints.BOTH. Or, if you don't mind every other component in the same container being just as large as your space-hungry component, you can use a GridLayout.

Scenario: You need to display a few components in a compact row at their natural size.
Consider using a JPanel to hold the components and using the JPanel's default FlowLayout manager.

Scenario: You need to display a few components of the same size in rows and columns.
GridLayout is perfect for this.

How to Create a Layout Manager and Associate It with a Container

Each container has a default layout manager associated with it. All JPanel objects are initialized to use a FlowLayout. The content pane for all JApplet objects and all JFrame objects are initialized to use a BorderLayout.

If you want to use a container's default layout manager, you don't have to do a thing. The constructor for each container creates a layout manager instance and initializes the container to use it.

To use a layout manager other than the default layout manager, you must create an instance of the desired layout manager class and tell the container to use it. The following statement creates a CardLayout manager and sets it up as the layout manager for a container.

aContainer.setLayout(new CardLayout());
[PENDING: add a new section that reflects how to change the layout manager for a container with a content pane.]

Rules of Thumb for Using Layout Managers

The Container methods that result in calls to the container's layout manager are add, remove, removeAll, doLayout, invalidate, getAlignmentX, getAlignmentY, getPreferredSize, getMinimumSize, and getMaximumSize. The add, remove, and removeAll methods add and remove components from a container; you can call them at any time. The doLayout method, which is called as the result of any paint request to a container or of a validate call on the container, requests that the container place and size itself and the components it contains; you don't call the doLayout method directly.

If you change the size of a component by indirect means, such as changing its font, you should invoke the invalidate method on the component. Then you should call validate on its container so that doLayout will be executed.

The getAlignmentX and getAlignmentY methods are called by layout managers that try to align groups of components. None of the layout managers in the 1.1 JDK calls these methods. [PENDING: BoxLayout calls this, right?]

The getPreferredSize, getMinimumSize, and getMaximumSize methods return the container's ideal, minimum, and maximum sizes, respectively. The values returned are just hints; a layout manager can ignore them.

Take special care when calling a container's getPreferredSize and getMinimumSize methods. The values these methods return are meaningless unless the container and its components have valid peer objects. See Details of the Component Architecture(in the Creating a User Interface trail) for information on when peers are created.


Previous | Next | Trail Map | Creating a User Interface (with Swing) | Laying Out Components within a Container