Custom Layout Wizardry
By
Al Williams
Sun's vision for Java has always been: "Write once, run anywhere." In practice, this may be somewhat optimistic. Still, Java is your best bet for running programs on multiple platforms -- that's why it's a success on the Web where there are dozens of different types of machines.
This machine independence poses some definite problems, however. If you develop on a Windows machine, it's difficult to tell how your program will look on a UNIX system, for example. Even on the same platform, variations in display hardware can affect your program's appearance. To combat this, Java programs use a layout manager that controls your program's appearance at run time.
Every AWT container (including an applet) has a layout manager -- the default is FlowLayout. When you add components (like buttons or list boxes) to the container, you can specify where you want each component to appear using arguments that vary between different layout managers. For example, the FlowLayout manager simply places the components side by side in the order you add them, starting a new row when it runs out of room. However, the BorderLayout manager lets you specify NORTH, SOUTH, EAST, WEST, or CENTER for each component. Other layout managers, like GridLayout and GridBagLayout, provide even more control over the placement of individual components (see last month's column, "In Search of... Visaj," for more information).
With the standard layout managers, you can usually create any visual arrangement you want.