14
JCAlignerLayout

Introduction · Sample Program · Aligning Labels

Resizing Height and Width · Example Programs


Introduction

The Java Development Kit contains a number of built-in layout managers (such as BorderLayout, FlowLayout and GridLayout). While each has its uses and advantages, none provide a simple way of arranging components in a columnar fashion that allows for an associated label. JCAlignerLayout is a layout manager ultimately derived from GridLayout that provides a simple way for programmers to lay out a vertically arranged group of control components, each with an associated label (or other component) placed to its left.

By default, a JCAlignLayout creates an aligner layout with 2 columns, a variable number of rows, with a gap of 5 pixels between each row. JCAlignerLayout can also be used to create an layout with a specified number of columns and gaps.

Complete reference documentation on JCAlignerLayout is available online in standard javadoc format in jclass.bwt.JCAlignerLayout.html.


Sample Program

The following code adds a JCLabel and a JCTextField to a JCAlignerLayout:

/**
 * This example demonstrates the use of a JCAlignerLayout
 */
public class alignerLayout extends java.applet.Applet {

public void init() {
	JCAlignerLayout layout = new JCAlignerLayout(2, 5, 5);
	JCLabel label;
	JCTextField text;

	setBackground(Color.lightGray);
	setLayout(layout);
	add(new JCLabel("Name"));
	add(new JCTextField("", 10));
	add(new JCLabel("Zip Code"));
	add(new JCTextField("", 10));
	add(new JCLabel("Phone"));
	add(text = new JCTextField("555-1212", 8));
	text.setMaximumLength(8);
	layout.setResizeWidth(text, true);
	add(label = new JCLabel("Address"));
	add(new JCTextArea(""));
	layout.setLabelVerticalAlignment(label, BWTEnum.TOP);
}

public static void main(String args[]) {
	ContribFrame frame = new ContribFrame("AlignerLayout");
	alignerLayout s = new alignerLayout();

	s.init();
	frame.add(s);
	frame.pack();
	frame.show();
}
}
When the code is compiled and run, the following display appears:

A sample program using the alighnerLayout layout manager

This sample code is incorporated into the example file alignerLayout.class provided with JClass BWT. For information on how to run this program, see the "Example Programs" section at the end of this chapter.


Aligning Labels

The LabelAlignment method sets the vertical position of a label (or other component) relative to its control. It can take one of three parameters: TOP, MIDDLE (default), or BOTTOM.


Resizing Height and Width

The ResizeWidth method determines whether or not a control should be resized horizontally to the right edge of its parent if it is in the final column. Similarly the ResizeHeight method sets whether a control should be resized vertically to the height of the largest component in its row. By default, the values for both ResizeWidth and ResizeHeight are false.


Example Programs

Demonstration programs and example code containing JCAlignerLayout are provided with JClass BWT. These examples can be viewed in applet form by launching index.html within the /jclass/bwt/examples directory. The alignerLayout.class program incorporates a JCLabel and a JCTextField. There are two other programs which demonstrate the uses to which JCAlignerLayout can be used: scrolledWindow.class (composed of a JCAlignerLayout and a JCScrolledWindow) and text.class (composed of a JCAlignerLayout containing JCTextField and JCTextArea components). These programs can be run as a stand-alone Java application from the command prompt by typing:

     java jclass.bwt.examples.alignerLayout
or
     java jclass.bwt.examples.scrolledWindow
or
     java jclass.bwt.examples.text