Flow Layout

The default layout manager used with Applets is FlowLayout. This is also true for Panels. This is not the case for Windows, Dialogs, and Frames. They all use something called BorderLayout.

There are five layout managers used in Java 1.1: FlowLayout, BorderLayout, GridLayout, GridBag Layout, and CardLayout. You also have the option of using no layout manager or creating your own layout manager. In this lesson we will discuss FlowLayout. Subsequent lessons will discuss BorderLayout, GridLayout, and using no Layout.


FlowLayout: When you use FlowLayout, components are added to a container from left to right and top to bottom in the order in which the add method is called for them in your program. Here's a quick example to make this clear:

As you can see the applet's add method was called first for the label, then for the textfield, then for the checkbox, and finally for the button. They are displayed in the applet in this same order. Now if we change the dimensions on the applet so that it is less wide, then we get the following result:
You will note that the components are still in the same order, but some got shuffled down to the next row since there wasn't room in the first row!
Constructors: There are three constructors used by the FlowLayout class:

1. FlowLayout(): This is the default constructor. By default the spacing between components is set to a gap of five pixels in both the horizontal and vertical directions. Also the default alignment used for the components is center (FlowLayout.CENTER).
2. FlowLayout(int align): This constructor maintains default spacing, but allows the user to specifiy alignment. The choices are FlowLayout.CENTER, FlowLayout.LEFT, and FlowLayout.RIGHT.
3. FlowLayout(int align, int hori, int vert): This constructor allows control of spacing and alignment. Spacing is in terms of pixels.


FlowLayout Methods: There are three interesting looking layout methods:

1. setAlignment(int align): This allows you to modify alignment.
2. setHgap(int hori): This allows you to change the horizontal spacing between components.
3. setVgap(): This allows you to change the vertical spacing between components.

In order to use them you have to declare an instance of FlowLayout. Normally you could just write:

      setLayout(new FlowLayout());

But if you want to use a FlowLayout method you have to:

      FlowLayout f = new FlowLayout();
      setLayout(f);

Here's an example which uses the FlowLayout methods:




Assignment: You will create an applet which contains nine buttons. Each button will have the name of some food on it. At the bottom of your applet you will have the words: "Pick your favorite". You will use drawString to do this in the paint method. You will size your applet so that the nine buttons line up in three rows of three. You will then make sure you have enough space left over at the bottom of your applet area for the "Pick your favorite" string. This will take some experimentation and adjustments, but it is not a hard assignment.