Creating and combining columns to form tables

Columns in the TableView are matched to the field of our Chapter class through reflection, so you need to use their fields names as string constants:

TableColumn<Chapter, String> columnTitle = new TableColumn<>("Title");
columnTitle.setCellValueFactory(
new PropertyValueFactory<>("title"));

TableColumn<Chapter, String> columnNumber = new TableColumn<>("#");
columnNumber.setCellValueFactory(
new PropertyValueFactory<>("number"));

Note we are using CellValueFactory, the same concept as in ListView, with the same capabilities for further customization.

Now, we can combine all these into a table:

TableView<Chapter> table = new TableView<>();
table.setItems(listChapters);
table.getColumns().add(columnNumber);
table.getColumns().add(columnTitle);

listChapters is being monitored by TableView and any changes in the list will be reflected in the table. You can see it for yourself by adding this code:

table.setOnMouseClicked((e)-> {
listChapters.add(new Chapter(14, "Future chapter"));
});
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.167.183