简体   繁体   中英

JavaFX - Automatically show alert box after closing it

I want to automatically show an alert box again after closing it, this is based on a certain condition. Here's my code:

protected void showInputDialog()
{
    FXMLLoader loader = new FXMLLoader(getClass().getResource("AddRecordDialog.fxml"));
    Parent root = loader.load();
    Scene scene = new Scene(root);
    AddRecordDialogController addRecordDialogController = (AddRecordDialogController)loader.getController();
    addRecordDialogController.setAddNewSalesDialogController(this);
    addRecordDialogController.setInvoice(this.invoice);
    this.addRecordDialog = new Stage();
    this.addRecordDialog.setTitle("Add Record");
    this.addRecordDialog.initModality(Modality.APPLICATION_MODAL);
    this.addRecordDialog.initOwner(root.getScene().getWindow());
    this.addRecordDialog.setScene(scene);
    this.addRecordDialog.sizeToScene();
    this.addRecordDialog.setResizable(false);
    //Event handler for when a Window is closed.
    this.addRecordDialog.setOnHiding(new EventHandler<WindowEvent>()
    {
        @Override
        public void handle(WindowEvent we)
        {
            if(nextItem == true)
               showInputDialog();
            nextItem = false;
        }
    });
    this.addRecordDialog.showAndWait();
}

The second dialog shows up but the first dialog doesn't disappear. The dialog is programmatically closed with a stage.close(); somewhere else. I saw here that you need to call the setOnHiding method for a programmatically closing event. If I remove the event handler the previous stage will close. But I want to open a new instance of that stage again after it is closed. Please help.

Edit: ...yes I checked for the nextItem variable it was true ,

I used System.out.println("Next Item: " + nextItem);

Based on kendavidson 's comment, I have found the solution. I've changed the code to:

        this.addRecordDialog.setOnHidden(new EventHandler<WindowEvent>()
        {
            @Override
            public void handle(WindowEvent we)
            {
                System.out.println("Next Item: " + nextItem);
                if(nextItem == true)
                {
                    nextItem = false;
                    showInputDialog();
                }
            }
        });

Thanks kendavidson.

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