13.32 loading and saving TodoItems part 2
Now that the data is being loaded from the file we can clean up the controller. The hard-coded TodoItems and the temporary line we used to bootstrap the file can be commented out. We still want to populate the ListView, but instead of using a local todoItems reference we call TodoData.getInstance().getTodoItems(). The rest of the initialization stays the same, so the ListView now displays whatever was loaded from disk.
Back in TodoData the temporary setTodoItems method that helped us seed the file is no longer used and can be removed. Running the app at this point should behave exactly as before, except the data really comes from the file.
Adding a File menu to the main window
With persistence working, we go back to the UI. We give the user a way to add a new to-do by attaching a menu bar to the top region of the BorderPane. In the FXML we add a <top> tag containing a <MenuBar>. An empty menu bar looks the same on screen, so we add a <Menu text="File"> inside it. Now clicking File still does nothing because we have not defined any items yet.
- Inside the Menu, add an
<items>tag. - Add a
<MenuItem text="New"/>. - Add a
<SeparatorMenuItem/>. - Add a second
<MenuItem text="Exit"/>.
Running the program now shows a File menu with New, a separator and Exit. Clicking the entries does nothing because we have not wired any event handlers, which is exactly what the next video focuses on so we can pop the new-item dialog.