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.

Summary

In this lesson, we extend the TodoItem class with three new instance variables: a short description, detailed notes, and a due date using Java's LocalDate class from the java.time package. We then move to the controller layer to establish the interaction between the user interface and the data model, implementing an initialize method that populates the application with sample TodoItem objects stored in an ArrayList.

Key points

  • Add three new fields to the TodoItem class: description (String), details (String), and dueDate (LocalDate) to enhance task information storage
  • Create constructors, getters, and setters for the expanded TodoItem class to manage these new properties
  • Understand the controller's role as an intermediary that manages interaction between the UI and the data model
  • Initialize the controller with sample TodoItem data using an ArrayList, such as birthday reminders, doctor appointments, and design proposals
  • Use Java's LocalDate.of() method to create specific due dates for each todo item
  • Build practical examples demonstrating how descriptions, details, and deadlines work together in the todo list application

FAQ

Why do we need both a description and details field for a todo item?

The description is a short summary displayed in the list view for quick scanning, while the details field stores extended information about the task, such as additional context, instructions, or important notes.

What is the purpose of using LocalDate from java.time, and how do we create date objects?

LocalDate is Java's modern date handling class that represents a date without time information, making it ideal for storing due dates and deadlines. We create specific dates using LocalDate.of() with the year, month, and day parameters (e.g., LocalDate.of(2022, 5, 12)).

What does the initialize method accomplish in the controller?

The initialize method sets up the application with sample TodoItem objects to demonstrate functionality, creating an ArrayList populated with example tasks that have descriptions, details, and due dates to show how the system works.