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();
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);

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

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

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

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);

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

Create the next column to display the car’s identification number:
table
.column(Car::number)
.name("Number")
.width(100);

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
