Class Table

java.lang.Object
com.codename1.cli.Table

public final class Table extends Object

Rows and columns rendered as text, aligned by the columns each cell takes on a terminal, so styled cells, wide characters and emoji line up:

Table table = new Table("Name", "Status", "Size")
        .align(2, Table.Align.RIGHT);
table.addRow("api", Console.style("running", Console.Style.GREEN), "12 MB");
table.addRow("worker", Console.style("stopped", Console.Style.RED), "3 MB");
Console.print(table.render());

A table wider than maxWidth (the terminal by default) shrinks its widest columns and wraps their cells. A cell may hold line breaks.

  • Constructor Details

    • Table

      public Table(String... headers)
      A table with these column headers; none for a table without a header row, whose column count then comes from its widest row.
  • Method Details

    • align

      public Table align(int column, Table.Align align)
      Aligns column column (from 0).
    • border

      public Table border(Table.Border border)
      The border style; Table.Border.ROUNDED by default, Table.Border.ASCII when Console.unicode() says box drawing is unsafe.
    • maxWidth

      public Table maxWidth(int maxWidth)
      The widest the table may be, in columns; the terminal width by default.
    • headerStyle

      public Table headerStyle(boolean bold)
      Whether the header row is bold (when color is on); true by default.
    • addRow

      public Table addRow(Object... cells)
      Appends a row. Each value is shown as String.valueOf gives it; a row shorter than the table is padded with empty cells.
    • rowCount

      public int rowCount()
      The number of rows added so far, header excluded.
    • render

      public String render()
      The table as text, every line ended by a line break.
    • toString

      public String toString()
      Description copied from class: Object
      Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method. The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of: getClass().getName() + '@' + Integer.toHexString(hashCode())
      Overrides:
      toString in class Object