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:
public TextAlignment(String left, String center, String right, int width); public TextAlignment(String left, String center, String right); public TextAlignment(String string, int alignment, int width); public TextAlignment(String string, int alignment);
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:
|<------ width ------>| |left center right| (left, center, right, width) |leftcenter | (left, center, null, width) |left | (left, null, null, width) | center | (null, center, null, width) | centerright| (null, center, right, width) |left right| (left, null, right, width) | right| (null, null, right, width) |leftcenterright (left, center, right, i) i < "leftcenterright".length()
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:
import java.io.PrintStream; import com.roguewave.format.TextAlignment; import com.roguewave.format.TextPicture; public class TextAlignmentExample2 { public static void main(String args[]) { printTable(System.out); } static TextAlignment center(String string) { return new TextAlignment(null, string, null); } static TextAlignment left(String string) { return new TextAlignment(string, null, null); } static void printRow(PrintStream ps, TextPicture fmt, String attr, String type, String dfault, String desc) { fmt.arg(left(attr)).arg(center(type)).arg(center(dfault)). arg(left(desc)).println(ps); } public static void printTable(PrintStream ps) { String picture = "| ______________ | _______ | _______ | ________________________________ |"; String hline = "-------------------------------------------------------------------------"; String hline2 = "|----------------|---------|---------|----------------------------------|"; TextPicture fmt = new TextPicture(picture); ps.println(hline); // Print titles centered within column: fmt.arg(center("Attribute")).arg(center("Type")).arg(center("Default")). arg(center("Description")).println(ps); ps.println(hline2); // Print rows: printRow(ps, fmt, "decimalSymbol", "String", ".", "what to print at the decimal point"); printRow(ps, fmt, "width", "int", "0", "width to pad to"); printRow(ps, fmt, "leftPadChar", "char", "<space>", "char to use for left padding"); ps.println(hline); } }
When compiled and run, this program will output:
---------------------------------------------------------------------------- | Attribute | Type | Default | Description | |----------------|---------|---------|-------------------------------------| | decimalSymbol | String | . | what to print at the decimal point | | width | int | 0 | width to pad to | | leftPadChar | char | <space> | char to use for left padding | ----------------------------------------------------------------------------
©Copyright 2000, Rogue Wave Software, Inc.
Contact Rogue Wave about documentation or support issues.