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.
Summary
This lesson continues building a JavaFX dialog for managing to-do items. You'll learn how to use FXMLLoader to load the dialog content, add fx:id identifiers to FXML elements, and declare them in the controller using @FXML annotations. The core focus is implementing a processResults() method that collects user input from text fields and a date picker, creates a new TodoItem object, and adds it to the application's data model.
Key points
- Use FXMLLoader.load() to dynamically load dialog content instead of hardcoded layouts
- Add fx:id attributes to FXML elements (descriptionField, detailsArea, dateLimitPicker) to make them accessible in the controller
- Declare FXML-connected fields with @FXML private annotations in the DialogController class
- Create a processResults() method that extracts text and date values, trims whitespace, and constructs new TodoItem objects
- Retrieve the dialog controller instance using FXMLLoader.getController() to call processResults() from the main window handler
- Add an addTodoItem() method to your data model (TodoData) to persist new items to the application's list
FAQ
Why use FXMLLoader instead of defining the dialog in code?
FXMLLoader allows you to load the dialog layout from an FXML file, keeping UI design separate from controller logic and making the dialog reusable and easier to maintain.
What does the @FXML annotation do?
The @FXML annotation marks private fields that will be automatically injected by the FXMLLoader with the corresponding UI elements from the FXML file based on their fx:id values.
How do you execute the dialog processing logic?
After getting the DialogController instance via FXMLLoader.getController(), you can call its processResults() method from the main controller's event handler when the OK button is clicked in the dialog.