Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8093619

[Dialog] No API for adding custom dialog validation and preventing dialog from being closed

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: P4
    • Resolution: Fixed
    • Affects Version/s: 8u40
    • Fix Version/s: 8u40
    • Component/s: javafx
    • Labels:
    • Environment:

      Mac OS X 10.9, Java 8u20, openjfx-dialog 1.0.2

      Description

      I'm using the openjfx-dialog backport library with JDK 8u20 on Mac OS X, the same problem exists with JDK 8u40 pre builds.

      By using the new Dialog API I've created a Dialog with many custom input controls, which needs to be validated when the user presses the OK button of the dialog.
      Unfortunately I can't find any API to execute the validation when the OK button was pressed. And there is also no API to prevent the dialog from being closed when validation errors have been found. Instead of closing the dialog I want to display an error message and the user needs to correct the invalid entries first.

      Now I found the following workaround by performing a lookup of the OK button and using an action filter which consumes the button action event in case of validation errors:


      final DialogPane dlgPane = dlg.getDialogPane();
      dlgPane.getButtonTypes().add(ButtonType.OK);
      dlgPane.getButtonTypes().add(ButtonType.CANCEL);

      final Button btOk = (Button) dlg.getDialogPane().lookupButton(ButtonType.OK);
      btOk.addEventFilter(ActionEvent.ACTION, (event) -> {
          if (!validateAndStore()) {
              event.consume();
          }
      });

      dlg.showAndWait();


      This solution works. But it would be much better to have an API for this common dialog scenario. It would also be great when its documented in the API doc of the dialog classes.

        Attachments

          Activity

            People

            Assignee:
            jgiles Jonathan Giles
            Reporter:
            ssaringjfx Stefan Saring (Inactive)
            Votes:
            1 Vote for this issue
            Watchers:
            3 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:
              Imported: