13 34 DialogPane2
We continue the dialog by populating its GridPane. The first row is the description: a Label with text "Description" placed at GridPane.rowIndex=0, columnIndex=0, followed by a TextField next to it. The second row holds a "Details" label and a TextArea, so users can type free-form text. The third row uses a "Deadline" label paired with a DatePicker. The basic input UI is now complete.
We do not declare OK/Cancel buttons in the FXML. JavaFX provides a built-in set of ButtonType entries that we will add from code in the main controller, because that is also where we own the event handler. Below initialize we add a new method public void showNewItemDialog() annotated with @FXML.
Creating the modal dialog and its owner
Inside the handler we instantiate Dialog<ButtonType> dialog = new Dialog<>(). By default a JavaFX Dialog is modal, so while it is showing the user cannot interact with the rest of the app. Setting the owner is good practice because it links the dialog back to its parent window. To get a reference to the parent we give the main BorderPane an id (fx:id="mainBorderPane"), then declare a matching @FXML private BorderPane mainBorderPane; in the controller.
- Call
mainBorderPane.getScene().getWindow()and pass it as the dialog owner. - Load the FXML with an
FXMLLoaderinside a try/catch that handlesIOException. - Attach the loaded root via
dialog.getDialogPane().setContent(root).
If loading fails we print a message saying the dialog could not be loaded. The next video continues by wiring the buttons and processing the user's input.
Summary
This lesson demonstrates how to build a complete dialog interface for adding new items in a JavaFX application. It covers adding form controls—labels, text fields, text areas, and date pickers—to a GridPane with proper row and column indexing. The lesson also explains how to instantiate a Dialog, configure it as modal (preventing interaction with the main window while open), assign FXML identifiers to elements for controller access, and properly load and display the FXML-defined UI within the dialog's content pane.
Key points
- Add form controls (Label, TextField, TextArea, DatePicker) to a GridPane using gridpane.rowIndex and gridpane.columnIndex properties
- Modal dialogs prevent user interaction with other application windows until closed via OK/Cancel buttons or the close button
- Assign IDs to FXML elements using fx:id annotation (e.g., mainBorderPane) to reference them in the controller
- Set the dialog's owner to the parent window using BorderPane.getScene().getWindow() to ensure proper modal behavior
- Load FXML files in the dialog method using FXMLLoader within a try-catch block to handle IOException
- Set the dialog's content using dialog.getDialogPane().setContent(root) to display the loaded UI
FAQ
What does it mean for a dialog to be modal in JavaFX?
A modal dialog blocks user interaction with other parts of the application while it is open. The user must close the dialog by clicking OK, Cancel, or the close button in the top-right corner before they can interact with the main window again.
Why do we assign an fx:id to the BorderPane in the FXML file?
We assign an fx:id to the BorderPane so we can reference it in the controller using the @FXML annotation. This allows us to get the parent window by calling BorderPane.getScene().getWindow(), which is necessary to set the dialog's owner.
What exception handling is required when loading FXML in the dialog method?
A try-catch block is required because the FXMLLoader.load() method can throw an IOException. The catch block should handle this exception and display an error message indicating that the dialog UI failed to load.