JDK-8173114 : "Abnormally Closing" javafx.scene.control.Dialog Doesn't Set the Result Property
  • Type: Bug
  • Component: javafx
  • Sub-Component: controls
  • Affected Version: 8,9
  • Priority: P4
  • Status: Open
  • Resolution: Unresolved
  • OS: windows_7
  • CPU: x86_64
  • Submitted: 2017-01-16
  • Updated: 2018-09-05
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
tbdUnresolved
Description
FULL PRODUCT VERSION :
java version "1.8.0_112"
Java(TM) SE Runtime Environment (build 1.8.0_112-b15)
Java HotSpot(TM) 64-Bit Server VM (build 25.112-b15, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows [Version 6.1.7601]

A DESCRIPTION OF THE PROBLEM :
The documentation for javafx.scene.control.Dialog indicates that when a dialog is abnormally closed, the result should be set to the first ButtonType with ButtonData that is either:

1) ButtonData.CANCEL_CLOSE, or
2) ButtonData.isCancelButton() is true.

However, testing reveals that abnormally closing a dialog follows this behavior only when the Result property is set to null.  If that property is not null, abnormally closing the dialog returns the value of that property when the dialog was shown.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run the source code provided.  Select the "Yes" button the first time the dialog is shown.  The second time the dialog is shown, click the small "x" in the upper-right of the dialog to abnormally close it.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The sample application should print the following to the console:

Yes
No or Closed
ACTUAL -
The sample application actually prints the following to the console:

Yes
Yes

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
package myPackage;

import java.util.Optional;

import javafx.application.Application;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.ButtonBar.ButtonData;
import javafx.scene.control.ButtonType;
import javafx.stage.Stage;

public class Main extends Application {

    public static void main(final String[] args) {
        launch();
    }

    private static boolean isYes(final Optional<ButtonType> result) {
        return (result.isPresent() && result.get().getButtonData() == ButtonData.YES);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        final Alert alert = new Alert(AlertType.CONFIRMATION,
            "This is a test", ButtonType.NO, ButtonType.YES);
        System.out.println(isYes(alert.showAndWait()) ? "Yes" : "No or Closed");
        System.out.println(isYes(alert.showAndWait()) ? "Yes" : "No or Closed");
    }

}
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Add the following immediately after creating a javafx.scene.control.Dialog:

Dialog dialog = new Alert(...);
dialog.setOnShowing(event -> dialog.setResult(null));


Comments
Verified the issue against 8u40,8u112,8u121,9ea on Windows 7 but could reproduce the issue as reported by the submitter Steps to reproduce(javac): ************************** - Run the attached file(Main.java) with JDK. Result: ********* OS : Windows 7 64-bit JDK : +++++++++++++++++++++++++ 8u40 b26 : Fail 8u112 b15 : Fail 8u121 b13 : Fail 9ea+152 : Fail ================================================================
20-01-2017