Your Own Java Layout Manager
Extending the Abstract Window Toolkit
By Cory Bear
While a small Java applet creates excitement with eye-catching animation or scrolling text, Java can be used for purposes more sophisticated than decorating your Web page. For example, larger applets with display components such as buttons, text fields, tables, and so on can be used as powerful, interactive user interfaces. The challenge in designing a large Java applet with many display components is finding a way to lay out those components attractively. How do you make text fields line up, or put a button in the correct spot? Java developers find the tools in the Abstract Window Toolkit (AWT) a big help, but many quickly learn that they need to design their own Java layout manager for advanced layout requirements. This article explains how I designed a layout manager called StackLayout and provides useful tips on designing your own layout manager. StackLayout stacks display components one on top of another, so they all have the same width but different heights. For example, it is used in the applet shown in
Figure 1 to lay out the "update" button, the "chart XYZ corp" text field, and a table of stock-quote information.
Java Layout Managers
Java provides five layout managers in the Abstract Window Toolkit (AWT), a package in the standard Java library: BorderLayout; CardLayout; FlowLayout; GridLayout; and GridBagLayout.