Text Alignment: Cell

The alignment of text can be set for individual table cells.

The following code creates a table, adds text objects, and then sets the text alignment for the last cell in second row to right leaving all other cells to the default justified alignment.

1
2
3
4
5
6
7
8
9
AsciiTable at = new AsciiTable();
String text = new LoremIpsum().getWords(9);
at.addRule();
at.addRow(text, text, text);
at.addRule();
AT_Row row = at.addRow(text, text, text);
at.addRule();
row.getCells().get(2).getContext().setTextAlignment(TextAlignment.RIGHT);
System.out.println(at.render(76));

The resulting output:

┌────────────────────────┬────────────────────────┬────────────────────────┐
│Lorem  ipsum  dolor  sit│Lorem  ipsum  dolor  sit│Lorem  ipsum  dolor  sit│
│amet,         consetetur│amet,         consetetur│amet,         consetetur│
│sadipscing elitr, sed   │sadipscing elitr, sed   │sadipscing elitr, sed   │
├────────────────────────┼────────────────────────┼────────────────────────┤
│Lorem  ipsum  dolor  sit│Lorem  ipsum  dolor  sit│   Lorem ipsum dolor sit│
│amet,         consetetur│amet,         consetetur│        amet, consetetur│
│sadipscing elitr, sed   │sadipscing elitr, sed   │   sadipscing elitr, sed│
└────────────────────────┴────────────────────────┴────────────────────────┘