0

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.

4

2 回答 2

3

编辑

我看到你发现了你的问题,我想我只是用示例代码保留我的答案,以防其他人有类似的问题。


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

这是我为测试它而创建的示例:

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); }
}

关闭时间输出

于 2013-07-24T21:01:57.513 回答
1

好的不要紧。事情解决了。对造成的不便表示歉意。问题是我将我的声明Stage为静态的。

于 2013-07-24T20:33:51.250 回答