title: What is the preferred size of a control?
body: <p>My first attempt at playing with JavaFX and I'm trying to understand a little bit about layout management. How do we access the preferred size of a control?</p>
  
  <p>In the example below I am attempting to set the maximum width 200 pixels greater than the preferred width. That is, I want to allow the button to grow (up to a maximum) as the width of the frame is increased.</p>
  
  <p>However when I run the code the preferred width is -1, so adding 200 to the preferred width gives a maximum width of 199.</p>
  
  <pre><code>import javafx.application.Application;
  import javafx.event.*;
  import javafx.stage.Stage;
  import javafx.scene.Parent;
  import javafx.scene.Scene;
  import javafx.scene.control.*;
  import javafx.scene.layout.*;
  import javafx.geometry.Insets;
  
  public class BorderPaneSSCCE extends Application
  {
      @Override
      public void start(Stage primaryStage)
      {
          Button button = new Button( "Button at PreferredSize" );
          button.setMaxWidth( button.getPrefWidth() + 200 );
          System.out.println(button.prefWidth(-1) + " : " + button.getPrefWidth());
  
          button.setOnAction(new EventHandler&lt;ActionEvent&gt;()
          {
              @Override
              public void handle(ActionEvent event)
              {
                  System.out.println("Width: " + button.getWidth());
              }
          });
  
          HBox root = new HBox();
          HBox.setHgrow(button, Priority.ALWAYS);
          root.getChildren().add(button);
  
          Scene scene = new Scene(root);
          primaryStage.setTitle("Java FX");
          primaryStage.setScene(scene);
          primaryStage.show();
          System.out.println(button.prefWidth(-1) + " : " + button.getPrefWidth());
      }
  
      public static void main(String[] args)
      {
          launch(args);
      }
  }
  </code></pre>
  
  <p>When I run the code and click on the button I see: <code>Width: 139.0</code></p>
  
  <p>After I resize the width of the frame so the button is as large as possible and I click on the button I see: <code>Width: 199.0</code></p>
  
  <p>I was hoping to see <code>Width: 339.0</code> (ie. 139 + 200)</p>
  
  <p>So, how do we access the preferred size/width of a control?</p>