JavaFX关闭应用程序模式对话框 [英] JavaFX closing application modal dialog

查看:150
本文介绍了JavaFX关闭应用程序模式对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用示例创建应用程序模式对话框.当我单击对话框上的退出按钮(右上角为红色)时,一切正常.对话框关闭,然后我可以正常打开它.但是,当我尝试添加一个Button来关闭对话框时,一切正常,直到我尝试重新打开它为止.之后,它抛出IllegalStateException(如果需要,我将使用此异常更新答案).

I'm using this example to create application modal dialog. When I click exit button on my dialog (red one in top right corner) everything works fine. Dialog gets closed and then I can open it normaly. But when I try to add a Button which closes my dialog, everything works fine until I try to reopen it. After that, it throws me a IllegalStateException (I'll update answer with this exception if needed).

这是一个事件处理程序,它演示了如何关闭对话框:

This is an event handler which demonstrates how I close a dialog:

    btnClose.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent event) {
            dialog.close();
        }
    });

有人可以告诉我如何正确关闭应用程序模式对话框吗?预先感谢.

Can someone tell me how to properly close application modal dialog? Thanks in advance.

推荐答案

编辑

我看到您找到了您的问题,我猜我只是保留了示例代码,以防其他人遇到类似的问题.

I see you found your issue, guess I just keep my answer with the sample code in case somebody else has a similar issue.

您的btnClose操作对我有用,因此问题可能出在您尚未发布的某些代码中.

Your btnClose action works for me, so the issue is probably in some code which you have not posted.

这是我创建的用于测试的示例:

Here is a sample I created to test it:

import javafx.application.Application;
import javafx.event.*;
import javafx.geometry.Pos;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.*;

public class DialogClosing extends Application {
    @Override public void start(final Stage stage) {
        final Button showDialog = new Button("Show Dialog");
        showDialog.setOnAction(new EventHandler<ActionEvent>() {
            @Override public void handle(ActionEvent actionEvent) {
                showDialog(stage, showDialog);
            }
        });

        StackPane layout = new StackPane();
        layout.getChildren().setAll(
            showDialog
        );

        layout.setStyle("-fx-padding: 10px;");
        stage.setScene(
            new Scene(
                layout
            )
        );
        stage.show();
    }

    private Stage showDialog(Window parent, final Node showControlNode) {
        showControlNode.setDisable(true);

        final Stage dialog = new Stage();
        dialog.initOwner(parent);
        dialog.initStyle(StageStyle.UTILITY);
        dialog.setX(parent.getX());
        dialog.setY(parent.getY() + parent.getHeight());

        Button closeDialog = new Button("Close Dialog");
        closeDialog.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                dialog.close();
            }
        });
        dialog.setOnHidden(new EventHandler<WindowEvent>() {
            @Override
            public void handle(WindowEvent windowEvent) {
                showControlNode.setDisable(false);
            }
        });

        VBox layout = new VBox(10);
        layout.setAlignment(Pos.CENTER);
        layout.getChildren().addAll(
            new Label("Hello World!"),
            closeDialog
        );
        layout.setStyle("-fx-padding: 10px;");

        Scene scene = new Scene(
            layout,
            125,
            100
        );

        dialog.setScene(scene);
        dialog.show();

        return dialog;
    }

    public static void main(String[] args) { launch(args); }
}

这篇关于JavaFX关闭应用程序模式对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆