Home
Email
Butnz!

How to Write a Layout Manager

Layout managers are damned useful pieces of code, but a little intimidating at first. You only have to implement five methods: So really there are only three functions to write. You need to know about the Component methods bounds(), which returns the current rectangle the component is in, and reshape() which changes that rectangle. In JDK 1.1, you should use getBounds() and setBounds() instead, but JDK 1.1-specific code won't run in applets in Netscape 3.0 and MSIE 3.0. You get the list of components to be laid out from the Container that you are passed. The methods you need to know in Container are countComponents(), getComponent() and getComponents(). Given all that info, you have to figure out where things will fit, how big they should be, and so on.

The Vertical Fill Layout Manager

This applet demonstrates three possible layouts of a set of components. The left one uses VFlowLayout, which is like a FlowLayout but is used to arrange components vertically. VFlowLayout is not standard, but I didn't write it either. VFlowLayout stacks components, placing each in its preferred vertical space. The centre one uses VFillLayout, which I did write. VFillLayout detects components which are TextAreas, Canvases or Panels, and expands them to fill the available space. You'll notice that the checkbox in the middle panel is lower than in the left panel, because VFillLayout has expanded the TextArea and the ImageCanvas to fill the column completely. In the green column, VFillLayout has been told not to expand the wizard, but rather to leave him in his natural size. This means the TextArea is the only component which can expand, and occupies more than its natural size.

One trick you can play with VFillLayout is to put an empty Canvas in the middle of the column. This canvas will then expand to fill available space, forcing remaining components to the bottom of the column. This wouldn't work with a VFlowLayout, because an empty canvas's natural height is 0 - the Canvas would disappear altogether. You can tell VFillLayout not to expand a component using the addException() method. VFillLayout also contains options dictating the horizontal behaviour of the components - whether they are left-aligned, right-aligned, centred, or fill the column. VFillLayout lays out invisible components, so if you have an invisible component in the middle of a VFillLayout, there will be a gap. I don't know if that's what's supposed to happen.

The Horizontal Fill Layout Manager

The horizontal fill layout manager, HFillLayout, works similarly to make a row of items fill a horizontal space. In this example, the top row is FlowLayout from the AWT. Even though there is space on the right, it does not expand the TextField. The HFillLayout in the centre row includes a TextField and an ImageCanvas, both of which it expands to fill the available space. Thus HFillLayout could be used to make a column of Label TextField pairs, where the labels are aligned on the left and the TextFields take the remaining space. Try doing that with GridLayout or FlowLayout! In the bottom row, I have again instructed the layout not to expand the image, making the TextField wider.

HFillLayout automatically expands TextAreas, TextFields, Canvases and Panels. You can instruct the layout not to expand a particular component using the addException() method. There are options controlling the vertical alignment of components - they can be top-aligned, bottom-aligned, centred or vertically filled. HFillLayout leaves a blank space for invisible components.


This page hosted by Get your own Free Home Page.
This page uses Verdana fonts which Windows users can download from Microsoft for free.
Background pattern by Net Creations.
Wizard image from Caboodles.

Friendless 1