13 23 TodoList part 2
We pick up our to-do list application where we left it. Each task needs to carry a short description shown in the list, extra details for the user to read, and a due date. We therefore add three fields to TodoItem:
public class TodoItem {
private String shortDescription;
private String details;
private LocalDate deadline; // from java.time
// constructor + getters + setters
}
We make sure to import java.time.LocalDate, then generate a constructor along with getters and setters via the IDE so the fields can be read and written normally.
Seeding the controller with example data
Back in the controller, we add a private list of TodoItem that the UI will display:
private List<TodoItem> todoItems;
To populate it, we add an initialize() method. initialize() is the standard JavaFX hook called after the FXML is loaded, and it is a convenient place to set up example data:
public void initialize() {
TodoItem item1 = new TodoItem(
"Birthday card",
"Buy a birthday card for John's 30th birthday.",
LocalDate.of(2022, 5, 12));
TodoItem item2 = new TodoItem(
"Doctor's appointment",
"See Dr. Smith at 123 Main Street. Bring papers.",
LocalDate.of(2022, 6, 20));
TodoItem item3 = new TodoItem(
"Final design proposal for the client",
"I promised Mike a website mockup by email by Friday May 22.",
LocalDate.of(2022, 5, 22));
todoItems = new ArrayList<>();
todoItems.add(item1);
todoItems.add(item2);
todoItems.add(item3);
}
We use LocalDate.of to build each deadline (year, month, day), and an ArrayList of TodoItem to hold the items. Now that the data model is in place and the controller has some sample tasks ready, we will stop here for this video. In the next one we will connect this list of items to the ListView on the UI side, so the tasks actually show up on screen. See you soon.