title: JavaFX equivalent of Swing's pack()
body: <p>I want to resize a window to fit the contents of the window. In Swing there is the <a href="http://docs.oracle.com/javase/6/docs/api/java/awt/Window.html#pack%28%29" rel="nofollow noreferrer"><code>pack()</code></a> method. Is there a similar method to do this in JavaFX?</p>
  
  <p>What I am trying to do is to create a confirmation dialog. When I create the dialog, it is wider than the contents, so I was asking myself if I need something like the pack method.</p>
  
  <p>Here is a screenshot of what is happening:
  <img src="https://i.stack.imgur.com/Ox3sH.png" alt="enter image description here"></p>
  
  <p>And here is my code:</p>
  
  <pre><code>mainClass.getPrimaryStage().setOnCloseRequest(new EventHandler&lt;WindowEvent&gt;() {
      @Override
      public void handle(final WindowEvent e) {
          e.consume();
  
          final Stage dialog = new Stage();
          dialog.setTitle("Confirm Before Exit");
          dialog.setResizable(false);
          dialog.initOwner(mainClass.getPrimaryStage());
          dialog.initModality(Modality.APPLICATION_MODAL);
  
          FlowPane buttons = new FlowPane(10,10);
          buttons.setAlignment(Pos.CENTER);
          Button yes = new Button("Yes");
          Button no = new Button("No");
          buttons.getChildren().addAll(yes, no);
          VBox box = new VBox();
          box.setAlignment(Pos.CENTER);
          box.setSpacing(10);
          box.getChildren().addAll(new Label("Do you really want to exit?"), buttons);
  
          yes.setOnAction(new EventHandler&lt;ActionEvent&gt;() {
              @Override
              public void handle(ActionEvent t) {
                  Platform.exit();
              }
          });
          no.setOnAction(new EventHandler&lt;ActionEvent&gt;() {
              @Override
              public void handle(ActionEvent t) {
                  dialog.close();
              }
          });
  
          Scene s = new Scene(box);
          dialog.setScene(s);
          dialog.show();
      }
  });
  </code></pre>
  
  <p>I hope they implement something like <a href="https://docs.oracle.com/javase/7/docs/api/javax/swing/JOptionPane.html" rel="nofollow noreferrer"><code>JOptionPane</code></a> in JavaFX soon! This is not something that I should be doing, it is so basic...</p>