Previous | Next | Trail Map | Creating a User Interface | Laying Out Components within a Container


Using Layout Managers

Every Container, by default, has a layout manager (an object conforming to the LayoutManager protocol). If a Container's default layout manager doesn't suit your needs, you can easily replace it with another one. We supply layout managers that range from the very simple (FlowLayout and GridLayout) to the special purpose (BorderLayout and CardLayout) to the ultra-flexible (GridBagLayout).

This lesson gives you some general rules for using layout managers, gives you an overview of the layout managers we provide, and then tells you how to use each of our layout managers. In these pages are applets illustrating layout management. Each applet brings up a window that you can resize to see how resizing affects the layout.

General Rules for Using Layout Managers

How do you choose a layout manager? How do you create it, associate it with a Container, and tell it to start working? How does it know what Components it manages? This section answers these and other questions that apply whenever you use a layout manager.

How to Use BorderLayout

BorderLayout is the default layout manager for all Windows, such as Frames and Dialogs. It 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.


Your browser can't run 1.0 Java applets, so here's a picture of the window the program brings up:


How to Use CardLayout

Use the CardLayout class when you have an area that can contain different Components at different times. CardLayouts are commonly tied to Choices, with the state of the Choice determining which Panel (group of Components) the CardLayout displays. Here's an applet that uses a Choice and CardLayout in this way.


Your browser can't run 1.0 Java applets, so here are pictures of the window the program brings up:


How to Use FlowLayout

FlowLayout is the default layout manager for all Panels. It simply lays out components from left to right, centering them within their row and, if necessary, starting new rows. Both Panels in the CardLayout figure above use FlowLayouts. Here's another example of an applet that uses a FlowLayout.


Your browser can't run 1.0 Java applets, so here's a picture of the window the program brings up:


How to Use GridLayout

GridLayouts simply make a bunch of Components have equal size, displaying them in the requested number of rows and columns. Here's an applet that uses a GridLayout to control the display of 5 buttons.


Your browser can't run 1.0 Java applets, so here's a picture of the window the program brings up:


How to Use GridBagLayout

GridBagLayout is the most sophisticated, flexible layout manager we provide. 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 a GridBagLayout to manage ten buttons in a panel.


Your browser can't run 1.0 Java applets, so here's a picture of the window the program brings up:



Previous | Next | Trail Map | Creating a User Interface | Laying Out Components within a Container