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

Trail: Creating a GUI with JFC/Swing

Lesson: Laying Out Components Within a Container

This lesson tells you how to use layout managers that the Java platform provides. It also tells you how to use absolute positioning (no layout manager) and gives an example of writing a custom layout manager. For each layout manager (or lack thereof), this section has an example that can run either as an applet or as an application. By resizing the window the example brings up, you can see how size changes affect the layout.

Before reading this section, you should understand basic layout management concepts, which you can find in Layout Management(in the Creating a GUI with JFC/Swing trail).


Note:  This lesson assumes you're using the Swing components. If you're using the AWT components instead, then you should read the old version of this lesson, which is available by download as part of the Creating a User Interface (AWT Only) archive.

Using Layout Managers

This section gives both general rules and detailed instructions on using each of the layout managers that the Java platform provides.

Creating a Custom Layout Manager

Instead of using one of the Java platform's layout managers, you can write your own. Layout managers must implement the LayoutManager interface, which specifies the five methods every layout manager must define. Optionally, layout managers can implement LayoutManager2, which is a subinterface of LayoutManager.

Doing Without a Layout Manager (Absolute Positioning)

If necessary, you can position components without using a layout manager. Generally, this solution is used to specify absolute sizes and positions for components.

Solving Common Layout Problems

Some of the most common layout problems are that components are displayed too small -- or not at all. This section tells you how to fix these and other common layout problems.

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