How to create Dialog in JavaFX

A dialog in JavaFX is a graphical user interface popup that inherits the main stage(Window). This article illustrates how to create a dialog in JavaFX.

In many occasions, you will always need dialog for various operations, that’s why it’s important to understand how to work with dialogs.

JavaFX Dialogs are available on the official JDK, preferably JDK 8 or new versions,

There are two types of dialogs in JavaFX:

  • Modal Dialogs
  • Standard Dialogs (Information Dialogs, warning dialogs, error dialogs, and exceptional dialogs)

Under this article, we will illustrate how to create Modal Dialogs (JDialog Version of JavaFX), for standard dialogs, which are JOptionPane version of JavaFX, refer to this tutorial.

How to Create Modal Dialogs in JavaFX

Many JavaFX beginners will ask, “How do I create a JDialog in JavaFX!”, anyway, that’s what we are demonstrating under this article.

JavaFX Dialogs uses the same code as opening a new stage, that means, JavaFX Dialog must have a Controller class and an FXML file. The following code illustrates how to open a JavaFX Dialog.

To illustrate how to create a JavaFX Dialog, I created a JavaFX project in eclipse, please see the image below.

JavaFX dialog project

From the above screenshot, we will focus on and DialogFxml.fxml

The following code illustrates how to launch a Dialog in JavaFX based on the above screenshot

public void openDialog(ActionEvent event) {
        FXMLLoader loader = new FXMLLoader();
  try {
    Pane pane = loader.load(getClass().getResource("DialogFxmla.fxml").openStream());
    Stage stage = new Stage();
    // now that we want to open dialog, we must use this line:
    stage.setScene(new Scene(pane));;
  } catch (IOException e) {


The openDialog method from the above code is the onAction method from your main stage, supplied from the main stage’s FXML file.

That’s how to open a dialog in JavaFX. If you need any clarifications, kindly use the commenting system below to ask any questions relating to this topic

Similar Posts