Read-only table

Read-only tables are constructed using the Table class.

Here is how we can create a simple table displaying a list of cars:

List<Car> cars = new ArrayList<>();
Table<Car> table = Tables
  .readonly(cars)
  .parent(parent)
  .create();
This table creation code snippet is part of the example GitHub project. You can find the Car and Person class definitions in the example project source code at GitHub. See also the sample data population code.

Table columns are created by calling the column() method.

The format(Function<T, String>) method accepts a function that converts objects to strings, defaulting to toString().

1. Column with color

Let’s create the first table column:

table
  .column(Car::color) // get color field
  .name("Color")
  .width(100);
Car color column with simple settings

We will now customize the column appearance:

  • Set each cell’s background color to match its car color

  • Since the color alone conveys the information, hide the text by applying format() with an empty string

table
  .column(Car::color)
  .name("Color")
  .width(100)
  .format(color -> "") // hide text
  .backgroundColor(Car::color); // set cell background color as the car color
Decorated car color column

2. Column with font

Create the second column with basic configuration to display the owner’s name.

table
  .column(Car::owner) // getting owner field from every car object
  .name("Owner")
  .width(100)
  .format(Person::getName); // show owner's name in cell
Car owner name simple column

Apply custom font (using the Font class) and font color:

Font font1 = new Font(parent.getDisplay(), "Arial", 7, 0);
Font font2 = new Font(parent.getDisplay(), "Times New Roman", 14, 2);
table
  .column(Car::owner)
  .name("Owner")
  .width(100)
  .format(Person::getName)
  .font(car -> car.owner().getAge() > 50 ? font1 : font2) // assign font
  .fontColor(car -> car.number() % 2 == 0 ? Color.RED : Color.BLUE); // assign font color
Owner column with font settings

3. Column with histogram

Create the next column to display the owner’s age:

table
  .column(car -> car.owner().getAge()) // getting age of owner
  .name("Owner age")
  .width(100);
Owner’s age column with simple settings

Enhance numeric data visualization by adding histogram bars to the column:

table
  .column(car -> car.owner().getAge())
  .name("Owner age")
  .width(100)
  .pseudoHistogramColor(car -> car.owner().getAge() % 2 == 0 ? Color.GREEN : Color.YELLOW); // assign pseudohistogram color with function defining color depending on number value
Owner’s age column with histogram

Create the next column to display the car’s identification number:

table
  .column(Car::number)
  .name("Number")
  .width(100);
Car number column with simple settings

4. Column with icon

Enhance cells by adding dynamic icons using the Image class. You can make the icon conditional based on the cell’s value.

private final Image saveImage = new Image( Display.getDefault(), getClass().getClassLoader().getResourceAsStream( "icons/save_edit.png" ) );
private final Image homeImage = new Image( Display.getDefault(), getClass().getClassLoader().getResourceAsStream( "icons/home.png" ) );
//	...

table
  .column(Car::number)
  .name("Number")
  .width(100)
  .icon(car -> car.number() > 100 ? homeImage : saveImage); // assign cell icon depending on car's number
Car number column with icons