8 Replies Latest reply: Jul 6, 2012 9:20 AM by PDVieira RSS

    What's the best way to create a Dialog

    PDVieira
      Hi,

      What's the best way to create a Dialog? I tried subclassing Stage but this way the window shows up on the taskbar of windows.

      Thanks in advance, cheers,
        • 1. Re: What's the best way to create a Dialog
          csh
          You can use

          initStyle(StageStyle.UTILITY);

          to avoid that.
          • 2. Re: What's the best way to create a Dialog
            PDVieira
            But I want it to be also transparent...
            • 3. Re: What's the best way to create a Dialog
              csh
              StageStyle.TRANSPARENT
              • 4. Re: What's the best way to create a Dialog
                csh
                StageStyle.TRANSPARENT
                • 5. Re: What's the best way to create a Dialog
                  PDVieira
                  Also Transparent as well as not showing up on the windows taskbar
                  • 6. Re: What's the best way to create a Dialog
                    jsmith
                    not showing up on the windows taskbar
                    Also invoke dialogStage.initOwner(parentStage)
                    http://docs.oracle.com/javafx/2/api/javafx/stage/Stage.html#initOwner%28javafx.stage.Window%29
                    • 7. Re: What's the best way to create a Dialog
                      JohnHendrikx
                      Perhaps this code is of some help.

                      It is intended to be subclassed for each type of dialog you want to create.

                      It creates a Dialog in the center of the Stage you specify when calling showDialog(). You can specify with the synchronous property if the showDialog method should block or not. It will add a fade-out effect to the parent to make the dialog clearly visible.
                      package hs.mediasystem.util;
                      
                      import javafx.animation.KeyFrame;
                      import javafx.animation.KeyValue;
                      import javafx.animation.Timeline;
                      import javafx.event.EventHandler;
                      import javafx.scene.effect.ColorAdjust;
                      import javafx.stage.Modality;
                      import javafx.stage.Stage;
                      import javafx.stage.StageStyle;
                      import javafx.stage.Window;
                      import javafx.stage.WindowEvent;
                      import javafx.util.Duration;
                      
                      public class DialogStage extends Stage {
                      
                        public DialogStage() {
                          super(StageStyle.TRANSPARENT);
                      
                          this.setTitle("MediaSystem-dialog");
                      
                          initModality(Modality.APPLICATION_MODAL);
                        }
                      
                        protected void setParentEffect(Stage parent) {
                          ColorAdjust colorAdjust = new ColorAdjust();
                      
                          Timeline fadeOut = new Timeline(
                            new KeyFrame(Duration.ZERO,
                              new KeyValue(colorAdjust.brightnessProperty(), 0)
                            ),
                            new KeyFrame(Duration.seconds(1),
                              new KeyValue(colorAdjust.brightnessProperty(), -0.5)
                            )
                          );
                      
                          parent.getScene().getRoot().setEffect(colorAdjust);
                      
                          fadeOut.play();
                        }
                      
                        protected void removeParentEffect(Stage parent) {
                          parent.getScene().getRoot().setEffect(null);
                        }
                      
                        protected void recenter() {
                          Window parent = getOwner();
                      
                          sizeToScene();
                      
                          setX(parent.getX() + parent.getWidth() / 2 - DialogStage.this.getWidth() / 2);
                          setY(parent.getY() + parent.getHeight() / 2 - DialogStage.this.getHeight() / 2);
                        }
                      
                        public final void showDialog(final Stage parent, boolean synchronous) {
                          initOwner(parent);
                      
                          setParentEffect(parent);
                      
                          setOnShown(new EventHandler<WindowEvent>() {
                            @Override
                            public void handle(WindowEvent event) {
                              recenter();
                              onShow();
                            }
                          });
                      
                          if(synchronous) {
                            showAndWait();
                          }
                          else {
                            show();
                          }
                        }
                      
                        protected void onShow() {
                        }
                      
                        @Override
                        public void close() {
                          removeParentEffect((Stage)getOwner());
                          super.close();
                        }
                      }
                      An example of an overriden version:
                      package hs.mediasystem.screens;
                      
                      import hs.mediasystem.util.DialogStage;
                      import javafx.event.EventHandler;
                      import javafx.scene.Scene;
                      import javafx.scene.control.Label;
                      import javafx.scene.input.KeyCode;
                      import javafx.scene.input.KeyCodeCombination;
                      import javafx.scene.input.KeyCombination;
                      import javafx.scene.input.KeyEvent;
                      import javafx.scene.layout.VBox;
                      import javafx.scene.paint.Color;
                      
                      public class InformationDialog extends DialogStage {
                        private static final KeyCombination BACK_SPACE = new KeyCodeCombination(KeyCode.BACK_SPACE);
                      
                        private final VBox root = new VBox();
                      
                        public InformationDialog(String text) {
                          root.getStylesheets().add("default.css");
                          root.getStylesheets().add("select-media/resume-dialog.css");
                          root.getStyleClass().add("resume-dialog");
                          root.getChildren().add(new Label(text));
                          root.setFillWidth(true);
                      
                          setScene(new Scene(root));
                      
                          getScene().setFill(Color.TRANSPARENT);
                          getScene().setOnKeyPressed(new EventHandler<KeyEvent>() {
                            @Override
                            public void handle(KeyEvent event) {
                              if(BACK_SPACE.match(event)) {
                                close();
                              }
                            }
                          });
                        }
                      }
                      • 8. Re: What's the best way to create a Dialog
                        PDVieira
                        Thanks for your answers! :)