简体   繁体   中英

JavaFX Abnormally Closing Alert Returns Incorrect Result

I have created a JavaFX Alert object that returns unexpected results when calling showAndWait . The code below illustrates the behavior I am observing:

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");
    }

}

When I run the above application, two dialogs are shown. Click "Yes" on the first dialog, then close (by clicking the "x" in the upper right corner) the second. By taking the above steps, I expect that the application would print the following:

Yes
No or Closed

However, what I am actually seeing printed is:

Yes
Yes

The Dialog documentation states that an "abnormal closing condition" (such as clicking the small "x" in the upper-right corner) will "attempt to set the result property to whatever value is returned from calling the result converter with the first matching ButtonType." Given the context of this statement, I interpreted "matching ButtonType" to mean a ButtonType that either (direct quote from the documentation):

  1. The button has a ButtonType whose ButtonBar.ButtonData is of type ButtonBar.ButtonData.CANCEL_CLOSE.
  2. The button has a ButtonType whose ButtonBar.ButtonData returns true when ButtonBar.ButtonData.isCancelButton() is called.

Is my interpretation of the documentation incorrect, or is this a bug in JavaFX? Regardless of why this is not working as I expect, is there any way I can force "abnormal closing conditions" to return ButtonType.NO in this case?

This is yet another bug in JavaFX. I have reported it to Oracle, and it was assigned Bug IDJDK-8173114 . As a work-around, I simply added the following line to the constructor(s) of my subclass of JavaFX Alert :

setOnShowing(event -> setResult(null));

The above work-around seems to work for Alert , ChoiceDialog , and TextInputDialog .

Above workaround does not work with java 8 & win 10. But this works:

    Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
   // alert.setOnShowing(event -> alert.setResult(null));
    alert.setTitle("Delete similar files?");
    ButtonType okButton = new ButtonType("Yes", ButtonBar.ButtonData.YES);
    ButtonType noButton = new ButtonType("No", ButtonBar.ButtonData.NO);
    ButtonType cancelButton = new ButtonType("Cancel", ButtonBar.ButtonData.CANCEL_CLOSE);
    alert.getButtonTypes().setAll(okButton, noButton, cancelButton);
    Optional<ButtonType> choose = alert.showAndWait();
    // String text = new String("");
    // if (choose.get() != null)
       // text = choose.get().getText();
    // if (text != null && (text.equals("YES") || text.equals("Yes")))
    if (choose.get() == okButton){

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM