13 36 Show Dialog2

We continue inside the try block. Instead of calling FXMLLoader.load(...) as a static method, we keep an FXMLLoader instance so we can also reach its controller. The line becomes dialog.getDialogPane().setContent(fxmlLoader.load()). Before grabbing the controller we need to give the input fields ids in todoItemDialog.fxml: fx:id="descriptionField" on the TextField, fx:id="detailsArea" on the TextArea and fx:id="deadlinePicker" on the DatePicker.

Back in the dialog controller we declare matching fields: @FXML private TextField descriptionField;, @FXML private TextArea detailsArea; and @FXML private DatePicker deadlinePicker;. Then we add a method public void processResults() that gathers the data and produces a new TodoItem.

processResults: gathering the user input

  • String description = descriptionField.getText().trim();
  • String details = detailsArea.getText().trim();
  • LocalDate deadlineValue = deadlinePicker.getValue();
  • Call TodoData.getInstance().addTodoItem(new TodoItem(description, details, deadlineValue));

The addTodoItem method does not exist yet, so we go back to TodoData and add a public void addTodoItem(TodoItem item) that simply calls todoItems.add(item). Back in the main controller, after loading the FXML, we grab the dialog controller with DialogController controller = fxmlLoader.getController(); and call controller.processResults(); when the OK button is pressed. There is still a small error when running the project. We will fix it in the next video.