简体   繁体   English

JavaFX - 关闭后自动显示警告框

[英]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(); 使用stage.close();以编程方式关闭对话框stage.close(); somewhere else. 别的地方。 I saw here that you need to call the setOnHiding method for a programmatically closing event. 我在这里看到你需要为编程关闭事件调用setOnHiding方法。 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 , 编辑: ...是的我检查了nextItem变量是true

I used System.out.println("Next Item: " + nextItem); 我使用了System.out.println("Next Item: " + nextItem);

Based on kendavidson 's comment, I have found the solution. 根据kendavidson的评论,我找到了解决方案。 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. 谢谢kendavidson。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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