As with a ListView, a TableView is backed up by ObservableList. But, to maintain the model-view connection, there are additional requirements for that list content:
- List elements should be instances of a class, containing table data
- Each element of the class should have a getter
Let's take a look at an example here of a class representing a chapter in this book:
// chapter10/table/TableViewDemo.java
class Chapter {
private final String title;
private final int number;
public Chapter(int number, String title) {
this.title = title;
this.number = number;
}
public String getTitle() {
return title;
}
public int getNumber() {
return number;
}
}
Using this object, we can create a list to be used later as data for a table:
private final ObservableList<Chapter> listChapters
= FXCollections.observableArrayList(
new Chapter(1, "Stages, Scenes and Layout"),
new Chapter(2, "Building Blocks: Shapes, Text and Controls"),
new Chapter(3, "Connecting Pieces: Binding"),
// ....