Text Alignment Options

Text in a table cell can be formatted using one of the four main supported text alignment options:

  • Left - left bound text

  • Right - right bound text, with added blanks left of the text to create the right alignment impression

  • Center - centered text, with added blanks left and right of the text to create the centered impression

  • Justified - justified text, with added blanks inside the text to create the justified impression

Justified alignment has two further specialized options:

  • Justified left - the last line of justified text will be aligned left

  • Justified right - the last line of justified text will be aligned right, with added blanks left of the text to create the right alignment impression

The following code creates a table with 2 rows and 3 columns each, inserts some text into the cells, and applies different text alignment options to each cell:

  • Cell 1-1: justified, lastl line left

  • Cell 1-2: justified

  • Cell 1-3: justified, las line right

  • Cell 2-1: left

  • Cell 2-2: centered

  • Cell 2-3: right

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
AsciiTable at = new AsciiTable();
String text = new LoremIpsum().getWords(19);
AT_Row row;
at.addRule();
row = at.addRow(text, text, text);
row.getCells().get(0).getContext().setTextAlignment(TextAlignment.JUSTIFIED_LEFT);
row.getCells().get(1).getContext().setTextAlignment(TextAlignment.JUSTIFIED);
row.getCells().get(2).getContext().setTextAlignment(TextAlignment.JUSTIFIED_RIGHT);
at.addRule();
row = at.addRow(text, text, text);
row.getCells().get(0).getContext().setTextAlignment(TextAlignment.LEFT);
row.getCells().get(1).getContext().setTextAlignment(TextAlignment.CENTER);
row.getCells().get(2).getContext().setTextAlignment(TextAlignment.RIGHT);
at.addRule();
System.out.println(at.render(79));

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│
│diam nonumy eirmod tempor│diam nonumy eirmod tempor│diam nonumy eirmod tempor│
│invidunt  ut  labore   et│invidunt  ut  labore   et│invidunt  ut  labore   et│
│dolore magna             │dolore              magna│             dolore magna│
├─────────────────────────┼─────────────────────────┼─────────────────────────┤
│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│
│diam nonumy eirmod tempor│diam nonumy eirmod tempor│diam nonumy eirmod tempor│
│invidunt ut labore et    │  invidunt ut labore et  │    invidunt ut labore et│
│dolore magna             │      dolore magna       │             dolore magna│
└─────────────────────────┴─────────────────────────┴─────────────────────────┘