Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8096336

Dialog opens non-centered to the stage, and gets more off-center the larger the dialog content

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 8u40
    • 8u40
    • javafx

      Issue:
      When a Dialog is opened it's not centered to the parent stage, no matter what its size is. When the content of the dialog gets larger, the off-center location gets increased.

      Code to reproduce. Click any button to see the issue.

      import javafx.application.Application;
      import javafx.scene.Scene;
      import javafx.scene.control.Button;
      import javafx.scene.control.ButtonType;
      import javafx.scene.control.Dialog;
      import javafx.scene.control.Label;
      import javafx.scene.layout.BorderPane;
      import javafx.scene.layout.VBox;
      import javafx.stage.Stage;
      import javafx.stage.Window;

      public class PopupLocationProblem extends Application {

      public static void main(String[] args) {
      launch(args);
      }

      @Override
      public void start(Stage primaryStage) throws Exception {
      BorderPane center = new BorderPane();
      Button showSmallPopup = new Button("Show Small Popup");
      showSmallPopup.setOnAction(e -> {
      showPopUp(primaryStage, true);
      });

      Button showBigPopup = new Button("Show Big Popup");
      showBigPopup.setOnAction(e -> {
      showPopUp(primaryStage, false);
      });

      VBox buttons = new VBox(showSmallPopup, showBigPopup);

      center.setCenter(buttons);

      Scene scene = new Scene(center);
      primaryStage.setScene(scene);
      primaryStage.setWidth(600);
      primaryStage.setHeight(400);
      primaryStage.show();
      }

      private void showPopUp(Window owner, boolean small) {
      Dialog<ButtonType> d = new Dialog<>();
      d.initOwner(owner);
      d.setTitle("Bad Centering - " + (small ? "Small" : "Big"));
      if (small) {
      d.getDialogPane().setContent(new Label("This is short."));
      } else {
      d.getDialogPane().setContent(new Label("This is a very long message to show how wrongly the popup positions itself when the content is large. The longer you make this text the more wrong it gets."));
      }
      d.getDialogPane().getButtonTypes().addAll(ButtonType.OK, ButtonType.CANCEL);
      d.showAndWait();
      }

      }

            jgiles Jonathan Giles
            ecrumhornjfx Emil Crumhorn (Inactive)
            Votes:
            1 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported: