7 Replies Latest reply: Jun 6, 2013 2:41 PM by James_D RSS

    Main stage is hidden when I press dialog button

    user10484841
      I tested this code:

      Main stage:
       primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>()
              {
                  @Override
                  public void handle(final WindowEvent e)
                  {
                      e.consume(); // Consuming the event by default.
                      new DialogPanels(primaryStage, "Question");
                  }
              });
      Close dialog:
      public class DialogPanels extends Stage
      {
      
          Stage owner;
          Stage stage;
          BorderPane root;
          //-----------------------------------------------------
      
          public DialogPanels(Stage owner, String title)
          {
              root = new BorderPane();
              stage = this;
              this.owner = owner;
              initModality(Modality.NONE);
              initOwner(owner);
              initStyle(StageStyle.UTILITY);
              setTitle(title);
              setContents();
          }
          //------------------------------------------------
      
          public void setContents()
          {
      
              Scene scene = new Scene(root, 150, 150);
              setScene(scene);
      
              Group groupInDialog = new Group();
              groupInDialog.getChildren().add(new Label("Really Exit ?"));
              root.setCenter(groupInDialog);
      
      
              Button yes = new Button("Yes");
              yes.setOnAction(new EventHandler<ActionEvent>()
              {
                  @Override
                  public void handle(ActionEvent paramT)
                  {
                      stage.close(); // Closing the pop up.
                      owner.close(); // Closing the parent stage also.
                  }
              });
      
              Button no = new Button("No");
              no.setOnAction(new EventHandler<ActionEvent>()
              {
                  @Override
                  public void handle(ActionEvent paramT)
                  {
                      stage.close(); // Closing the pop up only
                  }
              });
      
              HBox buttonPane = new HBox();
              buttonPane.setSpacing(10);
              buttonPane.getChildren().addAll(yes, no);
              root.setBottom(buttonPane);
      
              stage.showAndWait();
          }
      }
      When close the main syage I get modal dialog with question do I want to exit. When I press No the main stage is hides and the application is not closed normally. Any idea how I can fix this?
        • 1. Re: Main stage is hidden when I press dialog button
          James_D
          >

          >
          When close the main syage I get modal dialog with question do I want to exit. When I press No the main stage is hides and the application is not closed normally. Any idea how I can fix this?
          I get a non-model dialog (I can still go back to the main window and, for example, press the close button again to get a second dialog to appear). But otherwise, this behaves for me exactly as I would want. If I press "No", the dialog is dismissed but the main application remains. If I press yes, it exits.

          Complete example:
          import javafx.application.Application;
          import javafx.event.ActionEvent;
          import javafx.event.EventHandler;
          import javafx.scene.Group;
          import javafx.scene.Scene;
          import javafx.scene.control.Button;
          import javafx.scene.control.Label;
          import javafx.scene.layout.BorderPane;
          import javafx.scene.layout.HBox;
          import javafx.scene.layout.StackPane;
          import javafx.stage.Modality;
          import javafx.stage.Stage;
          import javafx.stage.StageStyle;
          import javafx.stage.WindowEvent;
          
          public class WindowCloseConfirmDialogTest extends Application {
          
            @Override
            public void start(final Stage primaryStage) {
          
          
              primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
                @Override
                public void handle(final WindowEvent e) {
                  e.consume(); // Consuming the event by default.
                  new DialogPanels(primaryStage, "Question");
                }
              });
          
              final StackPane root = new StackPane();
          
              final Scene scene = new Scene(root, 600, 400);
              primaryStage.setScene(scene);
              primaryStage.show();
            }
          
            public static void main(String[] args) {
              launch(args);
            }
          
            public class DialogPanels extends Stage {
          
              Stage owner;
              Stage stage;
              BorderPane root;
          
              // -----------------------------------------------------
          
              public DialogPanels(Stage owner, String title) {
                root = new BorderPane();
                stage = this;
                this.owner = owner;
                initModality(Modality.NONE);
                initOwner(owner);
                initStyle(StageStyle.UTILITY);
                setTitle(title);
                setContents();
              }
          
              // ------------------------------------------------
          
              public void setContents() {
          
                Scene scene = new Scene(root, 150, 150);
                setScene(scene);
          
                Group groupInDialog = new Group();
                groupInDialog.getChildren().add(new Label("Really Exit ?"));
                root.setCenter(groupInDialog);
          
                Button yes = new Button("Yes");
                yes.setOnAction(new EventHandler<ActionEvent>() {
                  @Override
                  public void handle(ActionEvent paramT) {
                    stage.close(); // Closing the pop up.
                    owner.close(); // Closing the parent stage also.
                  }
                });
          
                Button no = new Button("No");
                no.setOnAction(new EventHandler<ActionEvent>() {
                  @Override
                  public void handle(ActionEvent paramT) {
                    stage.close(); // Closing the pop up only
                  }
                });
          
                HBox buttonPane = new HBox();
                buttonPane.setSpacing(10);
                buttonPane.getChildren().addAll(yes, no);
                root.setBottom(buttonPane);
          
                stage.showAndWait();
              }
            }
          }
          • 2. Re: Main stage is hidden when I press dialog button
            user10484841
            Yes idea is to display dialog and still to have option to work with the main stage? Do you have any idea why when I click No the main stage is auto hidden?
            • 3. Re: Main stage is hidden when I press dialog button
              James_D
              user10484841 wrote:
              Yes idea is to display dialog and still to have option to work with the main stage?
              That seems strange for a "Really Quit" dialog, but maybe you need this for something else and this is just a quick demo? But OK...
              Do you have any idea why when I click No the main stage is auto hidden?
              In the code I posted, it doesn't; at least not on my system. Are you on Linux? If so you may be running into [url https://javafx-jira.kenai.com/browse/RT-24879]RT-24879.

              For a workaround, you could try an event filter instead:
              stage.addEventFilter(WindowEvent.WINDOW_CLOSE_REQUEST, new EventHandler<WindowEvent>() {
                @Override
                public void handle(WindowEvent event) {
                 // ...
                 event.consume();
                }
              });
              • 4. Re: Main stage is hidden when I press dialog button
                user10484841
                Yes I use Centos 6 64 bit. Maybe this is JavaFX issue. I tested your proposed code it's not working - I get the same result. I use JavaFX 2.2.21-b11

                Edited by: user10484841 on Jun 6, 2013 12:08 PM
                • 5. Re: Main stage is hidden when I press dialog button
                  James_D
                  Try it on JDK 8 (download [url https://jdk8.java.net/download.html]here), if only to verify that you're running into that bug (which is marked as fixed), and that we're not missing anything.
                  • 6. Re: Main stage is hidden when I press dialog button
                    user10484841
                    Yes on JDK 8 it's working properly. The bug is fixed. By the way may I ask you how to solve one issue: When I close the main stage the modal dialog which appears is not selected the main stage is still selected. How I can make the new dialog selected?
                    • 7. Re: Main stage is hidden when I press dialog button
                      James_D
                      Again, on my system the newly-created dialog gets the focus; so I can't really test this out.
                      Try calling stage.requestFocus() explicitly (or, failing that, call requestFocus() on one of the buttons).