This discussion is archived
0 Replies Latest reply: Jun 6, 2013 9:08 AM by user10484841 RSS

How to avoid scene reload after confirmation dialog

user10484841 Newbie
Currently Being Moderated
I tested this code:

Main scene:
public class DX57DC extends Application
{


    @Override
    public void start(Stage primaryStage)
    {

        // Set Main Window Label
        primaryStage.setTitle("Desktop Client");

        root = new BorderPane();            
        Scene scene = new Scene(root, xSize, ySize, Color.WHITESMOKE);  // Set main Stage color

        // Application Close Confirmation Dialog
        primaryStage.setOnHiding(new DialogPanels(primaryStage));

        primaryStage.setScene(scene);

        Rectangle2D primaryScreenBounds = Screen.getPrimary().getVisualBounds();

        // Set Stage boundaries to visible bounds of the main screen
        primaryStage.setX(primaryScreenBounds.getMinX());
        primaryStage.setY(primaryScreenBounds.getMinY());
        primaryStage.setWidth(primaryScreenBounds.getWidth());  // Maximum width of the display
        primaryStage.setHeight(primaryScreenBounds.getHeight());    // Maximum height of the display
        primaryStage.show();

    }

    public static void main(String[] args)
    {
        launch(args);
    }
Dialog code:
public class DialogPanels implements EventHandler<WindowEvent>
{

    private final Stage primaryStage;

    public DialogPanels(final Stage primaryStage)
    {
        Objects.requireNonNull(primaryStage);
        this.primaryStage = primaryStage;
    }

    @Override
    public void handle(final WindowEvent event)
    {

        event.consume();

        final Stage dialog = new Stage();

        final Button okBtn = new Button("Yes");
        okBtn.setOnAction(new EventHandler<ActionEvent>()
        {
            @Override
            public void handle(final ActionEvent event)
            {
                dialog.close();
                primaryStage.close();
            }
        });

        // Button "No"
        final Button cancelBtn = new Button("No");
        cancelBtn.setOnAction(new EventHandler<ActionEvent>()
        {
            @Override
            public void handle(final ActionEvent event)
            {
                dialog.close();

                Platform.runLater(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        primaryStage.show();
                    }
                });
            }
        });

        // Layout for the Button
        final HBox hbox = new HBox();
        hbox.setSpacing(10);
        hbox.setAlignment(Pos.CENTER);
        hbox.getChildren().add(okBtn);
        hbox.getChildren().add(cancelBtn);

        // Layout for the Label and hBox
        final VBox vbox = new VBox();
        vbox.setAlignment(Pos.CENTER);
        vbox.setSpacing(10);
        vbox.getChildren().add(new Label("Do your really want to exit?"));
        vbox.getChildren().add(hbox);

        // Stage
        final Scene scene = new Scene(vbox);
        dialog.setScene(scene);
        dialog.initOwner(primaryStage);

        dialog.initModality(Modality.NONE);

        dialog.showAndWait();
    }
}
I notice that when I click No button of the dialog the main stage is reloaded which is really uncomfortable for the user. Can I somehow avoid this?

Edited by: user10484841 on Jun 6, 2013 9:07 AM

Edited by: user10484841 on Jun 6, 2013 9:08 AM

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points