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.

Community
  • 1
  • 1
Branislav Lazic
  • 14,388
  • 8
  • 60
  • 85

2 Answers2

3

Edit

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


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

closingtimeoutput

jewelsea
  • 150,031
  • 14
  • 366
  • 406
1

Ok, nevermind. Things fixed. Sorry for inconvenience. The problem was I declared my Stage as static.

Branislav Lazic
  • 14,388
  • 8
  • 60
  • 85