简体   繁体   中英

alert type none closing javafx

I have a program with a menu bar that have an 'About' button to display some info about the app.

The thing is that when I use AlertType.INFORMATION I can hit Ok button to close the alert, but when i use NONE when i hit the close window button, nothing happens. I already tried to put a setOnCloseAction(e-> close()); but it dont close either.

Thanks!

public class RootLayoutController {

private MainApp main;

@FXML
private MenuItem loadFiles;

@FXML
private MenuItem about;

@FXML
private void displayAbout() {
    Alert alert = new Alert(AlertType.NONE);
    alert.initStyle(StageStyle.UTILITY);
    alert.initOwner(main.getPrimaryStage());
    alert.setTitle("Organiz3r");
    alert.setHeaderText("Organiz3r v1.0");
    alert.setContentText("Developed at BitBucket");
    alert.showAndWait();
}

@FXML
private void handleLoad() {
    FileChooser fileChooser = new FileChooser();
    fileChooser.setTitle("Open Files");
    List<File> files = fileChooser.showOpenMultipleDialog(main.getPrimaryStage());
    main.loadFiles(files);
}

public RootLayoutController() {
    // TODO Auto-generated constructor stub
}

public void setMain(MainApp main) {
    this.main = main;
}

Main is setted at main class with

// Load root layout from fxml file.
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(MainApp.class.getResource("view/RootLayout.fxml"));
        rootLayout = (BorderPane) loader.load();

        RootLayoutController controller = loader.getController();
        controller.setMain(this);

The documentation explains (in the section "Dialog Closing Rules") that pressing the window close button will have no effect unless there is either exactly one button, or there are two or more buttons, one of which is essentially a "Cancel" button. Thus when you create an Alert with AlertType.NONE , it has no buttons and so closing it with the standard "window close" button will be ignored.

So, if you do not want an AlertType.INFORMATION , you need to add a button to your Alert, which you can do with

alert.getDialogPane().getButtonTypes().add(ButtonType.OK);

Based on the Dialog documentation it seems like you must have at least one button in the Dialog/Alert in order to close it with the "x" button in the corner. According to the docs, closing with the "x" button is considered closing "abnormally." Here's what it says:

JavaFX dialogs can only be closed 'abnormally' (as defined above) in two situations:

When the dialog only has one button, or

When the dialog has multiple buttons, as long as one of them meets one of the following requirements:

The button has a ButtonType whose ButtonBar.ButtonData is of type ButtonBar.ButtonData.CANCEL_CLOSE.

The button has a ButtonType whose ButtonBar.ButtonData returns true when ButtonBar.ButtonData.isCancelButton() is called. In all other situations, the dialog will refuse to respond to all close requests...

You could go with an AlertType.INFORMATION and then hide the 'OK' button. This enables you to close the window with the 'x' button from the corner without having another button.

dialogPane.lookupButton(ButtonType.OK).setVisible(false);

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