Rogue Wave banner
Previous fileTop of DocumentContentsIndexNext file

8.4 TextAlignment

A TextAlignment object allows text to be laid out left, center, or right justified. By nesting TextAlignment objects you can achieve arbitrary formats. Padding is inserted evenly among the null fields or between left and center, and center and right, if no fields are null.

A TextAlignment object has three fields: left, center, and right. With one or more of these fields set, the TextAlignment object provides padding to justify the text in a field of a given width.

The TextAlignment class has four constructors:

For the first two constructors, each field is assigned either a string value, which can be the empty string "", or a null. Fields given a null value indicate where padding should occur. If no null fields are specified, then padding occurs to either side of the center field. The last two constructors provide a simplified way to specify a layout with only one field. The first and third constructors specify a width and should be used with the toString() method; the second and fourth constructors do not, and should be used with the toString(int width) method. The alignment argument can have one of three values defined in the TextAlignment class: LEFT, CENTER, and RIGHT.

Here are the various permutations and the resulting output:

The example program called TextAlignmentExample.java prints out something similar to the above. The program TextAlignmentExample2.java, which follows, combines the use of TextPicture and TextAlignment.


NOTE: Complete code for these examples is located in the examples directory created for your installation of Tools.h++ Professional. The "Examples" chapter in Part V, "Resources," describes the location of that directory, or you can check the online build guide for your installation media.

Here is the code for TextAlignmentExample2.java:

When compiled and run, this program will output:


Previous fileTop of DocumentContentsIndexNext file

©Copyright 2000, Rogue Wave Software, Inc.
Contact Rogue Wave about documentation or support issues.