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

Alert class Javadoc example needs improvement

    XMLWordPrintable

    Details

    • Type: Enhancement
    • Status: Resolved
    • Priority: P5
    • Resolution: Fixed
    • Affects Version/s: 8u40
    • Fix Version/s: 8u40
    • Component/s: javafx
    • Labels:

      Description

      In the class description for the javafx.scene.control.Alert class there is a code example like this:

       * <p><strong>Option 1: The 'traditional' approach</strong>
       * <pre>{@code Optional<ButtonType> result = alert.showAndWait();
       * if (result.get() == ButtonType.OK) {
       * formatSystem();
       * }}</pre>

      If the user closes the alert dialog without clicking a button (for example, using the little red cross at the top right of the dialog window on Windows), the following exception is thrown:

      java.util.NoSuchElementException: No value present
      at java.util.Optional.get(Optional.java:135) ~[?:1.8.0_40-ea]

      To avoid this, the check on the result would be better written as follows:

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

        Attachments

          Activity

            People

            Assignee:
            jgiles Jonathan Giles
            Reporter:
            dgilbertjfx David Gilbert (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:
              Imported: