JavaFX: warning automatically closes dialog

I am a beginner creating a simple hands-on application in JavaFX. I am using a dialog box with 3 text boxes and a dumper to create the "Items" to be added as records in the SQLite db.

I am trying to use an alert to validate data. If one or more of the fields are empty and you click OK in the dialog box, a warning appears. The problem is that closing the alert also closes the dialog.

How can I display a warning and be closed without causing the dialog to close as well?


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

 @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 controller that contains the warning:

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();


    }

}

      


Thanks for your time and all the answers.

+3


source to share


1 answer


You can intercept the ButtonType.OK

action Dialog

. Try it.

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 the dialog 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 is described in the Dialog

API document. Actions with dialog check / interception of actions

+1


source







All Articles