Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX: alert closing the dialog box automatically

I'm a beginner creating a simple practice application in JavaFX. I use a dialog box with 3 textfields and a datepicker to create "Items" to be added as entries in an SQLite db.

I'm trying to use an alert for data validation. If one or more fields are empty this and the OK button is pressed on the dialog box, the alert pops up. The problem is that closing the alert also closes the dialog box.

How can I have the alert display and be closed, without that causing the dialog box to close also?


This is the method I use for the "new item" button in the main window controller, this brings up the dialog box:

 @FXML
public void newItem() {

    FXMLLoader fxmlLoader = new FXMLLoader();
    fxmlLoader.setLocation(getClass().getResource("newEventDialog.fxml"));
    try {
        dialog.getDialogPane().setContent(fxmlLoader.load());
    } catch (IOException e) {
        System.out.println("Error loading new Dialog : " + e.getMessage());
    }

    newEventDialogController newController = fxmlLoader.getController();

    dialog.getDialogPane().getButtonTypes().addAll(ButtonType.OK, ButtonType.CANCEL);


    Optional<ButtonType> result = dialog.showAndWait();


    if (result.isPresent() && result.get() == ButtonType.OK) {

            newController.addItem();
            refreshList();



    }
}

This is the method inside the Dialog's controller that contains the alert:

public void addItem() {
    if (validateFields()) {

        String eventdate = datepick.getValue().format(DateTimeFormatter.ofPattern("dd/MM/yyyy"));

        Item item = new Item(namefield.getText(), emailfield.getText(), typefield.getText(), eventdate);
        Datasource.getInstance().insertEvent(item);
    } else {
        Alert alert = new Alert(Alert.AlertType.ERROR);

        alert.setContentText("Error: One or more fields are empty.");
        alert.showAndWait();


    }

}

Thank you for your time, and any and all replies.

like image 210
Alin Avatar asked Dec 04 '25 21:12

Alin


1 Answers

You can intercept ButtonType.OK's action of the Dialog. Try this.

dialog.getDialogPane().getButtonTypes().addAll(ButtonType.OK, ButtonType.CANCEL);
final Button btOk = (Button)dialog.getDialogPane().lookupButton(ButtonType.OK);
btOk.addEventFilter(ActionEvent.ACTION, event -> {
   if (newController.addItem()) {
       refreshList();
   } else {
       event.consume();  // Make dialog NOT to be closed.
   }
});

Optional<ButtonType> result = dialog.showAndWait();

in Dialog's controller

// Return false, if you want NOT to close dialog.
public boolean addItem() {
    if (validateFields()) {
        String eventdate = datepick.getValue().format(DateTimeFormatter.ofPattern("dd/MM/yyyy"));
        Item item = new Item(namefield.getText(), emailfield.getText(), typefield.getText(), eventdate);
        Datasource.getInstance().insertEvent(item);
        return true;
    } else {
        Alert alert = new Alert(Alert.AlertType.ERROR);
        alert.setContentText("Error: One or more fields are empty.");
        alert.showAndWait();
        return false;
    }
}

This approach has been described in Dialog API Document. Dialog Validation / Intercepting Button Actions

like image 131
monolith52 Avatar answered Dec 06 '25 14:12

monolith52