Columns

The number of columns of a table is automatically set by the first content row added to the table. If the first row has 1 text object, then the table has 1 column. If the first row has 2 text objects, then the table has 2 columns, and so forth.

The following examples show this behavior for 1 to 5 columns.

1 Column

We start with 1 column be adding 1 text object to a table.

1
2
3
4
5
6
7
8
9
AsciiTable at = new AsciiTable();
at.addRule();
at.addRow("Table Heading");
at.addRule();
at.addRow("first row (col1)");
at.addRule();
at.addRow("second row (col1)");
at.addRule();
System.out.println(at.render(30));

The resulting output shows that a table with 1 column:

┌────────────────────────────┐
│Table Heading               │
├────────────────────────────┤
│first row (col1)            │
├────────────────────────────┤
│second row (col1)           │
└────────────────────────────┘

2 Columns

Next, we create a new table and add 2 columns.

1
2
3
4
5
6
7
AsciiTable at = new AsciiTable();
at.addRule();
at.addRow("first row (col1)", "some information (col2)");
at.addRule();
at.addRow("second row (col1)", "some information (col2)");
at.addRule();
System.out.println(at.render(65));

The resulting output shows that a table with 2 columns:

┌───────────────────────────────┬───────────────────────────────┐
│first row (col1)               │some information (col2)        │
├───────────────────────────────┼───────────────────────────────┤
│second row (col1)              │some information (col2)        │
└───────────────────────────────┴───────────────────────────────┘

3 Columns

Next, we create a new table and add 3 columns.

1
2
3
4
5
6
7
AsciiTable at = new AsciiTable();
at.addRule();
at.addRow("first row (col1)", "some information (col2)", "more info (col3)");
at.addRule();
at.addRow("second row (col1)", "some information (col2)", "more info (col3)");
at.addRule();
System.out.println(at.render());

The resulting output shows that a table with 4 columns:

┌──────────────────────────┬─────────────────────────┬─────────────────────────┐
│first row (col1)          │some information (col2)  │more info (col3)         │
├──────────────────────────┼─────────────────────────┼─────────────────────────┤
│second row (col1)         │some information (col2)  │more info (col3)         │
└──────────────────────────┴─────────────────────────┴─────────────────────────┘

4 Columns

Next, we create a new table and add 4 columns.

1
2
3
4
5
6
7
AsciiTable at = new AsciiTable();
at.addRule();
at.addRow("first row (col1)", "text (col2)", "more text (col3)", "even more (col4)");
at.addRule();
at.addRow("second row (col1)", "text (col2)", "more text (col3)", "even more (col4)");
at.addRule();
System.out.println(at.render());

The resulting output shows that a table with 4 columns:

┌───────────────────┬───────────────────┬───────────────────┬──────────────────┐
│first row (col1)   │text (col2)        │more text (col3)   │even more (col4)  │
├───────────────────┼───────────────────┼───────────────────┼──────────────────┤
│second row (col1)  │text (col2)        │more text (col3)   │even more (col4)  │
└───────────────────┴───────────────────┴───────────────────┴──────────────────┘

5 Columns

Next, we create a new table and add 5 columns.

1
2
3
4
5
6
7
AsciiTable at = new AsciiTable();
at.addRule();
at.addRow("row1 (col1)", "text (col2)", "text (col3)", "text (col4)", "text (col5)");
at.addRule();
at.addRow("row2 (col1)", "text (col2)", "text (col3)", "text (col4)", "text (col5)");
at.addRule();
System.out.println(at.render());

The resulting output shows that a table with 5 columns:

┌───────────────┬───────────────┬───────────────┬───────────────┬──────────────┐
│row1 (col1)    │text (col2)    │text (col3)    │text (col4)    │text (col5)   │
├───────────────┼───────────────┼───────────────┼───────────────┼──────────────┤
│row2 (col1)    │text (col2)    │text (col3)    │text (col4)    │text (col5)   │
└───────────────┴───────────────┴───────────────┴───────────────┴──────────────┘