We will now learn how to make it so that our program populates the table view on the to do list page.
Each time a page is called, the controller will execute an initialize() method. This method has following header format.
@FXML
private void initialize() {
//What needs to be carried out when a page is called.
}
1. We will create an initialize function for the to do list page which will create the content for the table view. In order to do so place an initialize() method in the ToDoListController class.
2. This function is going to need to know the table it is going to update as well as the corresponding columns. Remember when we gave the table and columns fx:ids? Place the following attributes at the top of the ToDoListController class.
@FXML
private TableView toDoTable;
@FXML
private TableColumn doneColumn;
@FXML
private TableColumn taskColumn;
@FXML
private TableColumn taskNotesColumn;
NOTE: Look at the TableView declaration. TableView< x > where x is the type of objects that are contained within the tableview, so for this declaration x=ToDo. Now look at the TableColumn's declaration. TableColumn < y, z > , y is the TableView's generic type, which for this tableview is ToDo. z is the type of content that will be held in the column which in this case will be a String.
3. Now we need the initialize method to actually do something with these fx:ids. For this, let us take a look at the table column API. Specifically look at the method setCellValueFactory(). This method is a setter that provides TableColumn.CellDataFeatures which is a wrapper class that holds all the information required by the table column to fill it. This sounds like exactly what we need. Place 3 method calls for all 3 columns to setCellValueFactory().
@FXML
private void initialize() {
doneColumn.setCellValueFactory();
taskColumn.setCellValueFactory();
taskNotesColumn.setCellValueFactory();
}
4. Now, what input do we provide these functions calls to successfully fill the table? According to the JavaFX API there are two ways, we can provide a callback or a PropertyValueFactory. We will be using a PropertyValueFactory class. This is used for its relative simplicity. Look below to see the implementation.
private void initialize() {
doneColumn.setCellValueFactory(new PropertyValueFactory<ToDo, String>("doneString"));
taskColumn.setCellValueFactory(new PropertyValueFactory<ToDo, String>("taskName"));
taskNotesColumn.setCellValueFactory(new PropertyValueFactory<ToDo, String>("taskNotes"));
}
NOTE:Let us explain some of the parameters of PropertyValueFactory<x, y>(z). Similar to the table column's values, x is the TableView's generic type, and y is the type of class that will be held in the cells. z is the property that will be placed inside the collumn.
Congratulations! You've completed the method that will populate out tableview. Try out the new application. Just to make sure, your initialize function and new attributes should resemble the code shown below.
public class ToDoListController {
@FXML
private TableView toDoTable;
@FXML
private TableColumn doneColumn;
@FXML
private TableColumn taskColumn;
@FXML
private TableColumn taskNotesColumn;
private TooToDoMain main;
@FXML
private void initialize() {
doneColumn.setCellValueFactory(new PropertyValueFactory("doneString"));
taskColumn.setCellValueFactory(new PropertyValueFactory("taskName"));
taskNotesColumn.setCellValueFactory(new PropertyValueFactory("taskNotes"));
}
//rest of file