Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
like image 465
jisuskraist Avatar asked Dec 09 '22 01:12

jisuskraist


2 Answers

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 anAlertType.INFORMATION, you need to add a button to your Alert, which you can do with

alert.getDialogPane().getButtonTypes().add(ButtonType.OK);
like image 196
James_D Avatar answered Dec 10 '22 14:12

James_D


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...

like image 31
Amber Avatar answered Dec 10 '22 14:12

Amber