The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Trail: Creating a GUI with JFC/Swing
Lesson: User Interfaces that Swing: A Quick Start Guide

Layout Management

The following figure shows the GUIs of five programs, each of which displays five buttons. The buttons are identical, and the code for the programs is almost identical. So why do the GUIs look so different? Because they use different layout managers to control the size and the position of the buttons.

Five examples of layout management.

The Java platform supplies five commonly used layout managers: BorderLayout, BoxLayout, FlowLayout, GridBagLayout, and GridLayout.

Using Layout Managers

By default, every container has a layout manager. All JPanel objects use a FlowLayout by default, whereas content panes (the main containers in JApplet, JDialog, and JFrame objects) use BorderLayout by default. As a rule, the only time you have to think about layout managers is when you create a JPanel or add components to a content pane. If you don't like the default layout manager that a panel or content pane uses, you can change it to a different one. Just invoke the container's setLayout method. For example, here's the code that makes a panel use BorderLayout:
JPanel pane = new JPanel();
pane.setLayout(new BorderLayout());
When you add components to a panel or a content pane, the arguments you specify to the add method depend on the layout manager that the panel or content pane is using. So be sure to check the API documentation for the layout manager for details.

Here's a quick summary of the various layout managers and where to find more information in The Java Tutorial and API documentation.

BorderLayout
BorderLayout is the default layout manager for every content pane. The content pane is the main container in all frames, applets, and dialogs. A BorderLayout has five areas available to hold components: north, south, east, west, and center. All extra space is placed in the center area.

BorderLayout

Tutorial: How to Use BorderLayout(in the Creating a GUI with JFC/Swing trail) API documentation: BorderLayout(in the API reference documentation)

BoxLayout
The BoxLayout class puts components in a single row or column. This class respects the components' requested maximum sizes and also lets you align components.

BoxLayout

Tutorial: How to Use BoxLayout(in the Creating a GUI with JFC/Swing trail) API documentation: BoxLayout(in the API reference documentation)

FlowLayout
FlowLayout is the default layout manager for every JPanel. This layout manager simply lays out components from left to right, starting new rows, if necessary.

FlowLayout

Tutorial: How to Use FlowLayout(in the Creating a GUI with JFC/Swing trail) API documentation: FlowLayout(in the API reference documentation)

GridLayout
GridLayout simply makes a bunch of components equal in size and displays them in the requested number of rows and columns.

GridLayout

Tutorial: How to Use GridLayout(in the Creating a GUI with JFC/Swing trail) API documentation: GridLayout(in the API reference documentation)

GridBagLayout
GridBagLayout is the most sophisticated, flexible layout manager the Java platform provides. This layout manager 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.

GridBagLayout

Tutorial: How to Use GridBagLayout(in the Creating a GUI with JFC/Swing trail) API documentation: GridBagLayout(in the API reference documentation)


Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Copyright 1995-2001 Sun Microsystems, Inc. All rights reserved.