3 Replies Latest reply: Nov 9, 2012 8:34 AM by bjmueller RSS

    From which moment on do prefWidth() and prefHeight() return correct results

    bjmueller
      The functions Node.prefWidth() and Node.prefHeight() return 0 after creating a node (e.g. after new Button("xyz"). - Then "suddenly" (e.g. when being positioned in layoutChildren() of a pane) they return back correct values.

      Which is the trigger for making nodes return a correct prefWidth/prefHeight result? Is there some way to directly get correct prefWidth/prefHeight just after creating a component instance?

      Thanks for any hint on this!



      Some example: an own pane-extension with simple layoutChildren() method into which three buttons are added. The prefWidth/prefHeight is output to the console:
      public class Test_39_PrefWidth extends Application
      {
          public static void main(String[] args) { launch(args); }
      
          class MyPane extends Pane
          {
              protected void layoutChildren()
              {
                  double y = 0;
                  sysoutNodePrefSizes("----- doing the layout");
                  for (Node child: getChildren())
                  {
                      double childWidth = child.prefWidth(-1);
                      double childHeight = child.prefHeight(-1);
                      child.resizeRelocate(0,y,childWidth,childHeight);
                      y = y + childHeight;
                  }
              }
          }
          
          MyPane m_pane;
          
          public void start(Stage primaryStage)
          {
              final Scene scene = SceneBuilder.create()
                   .root
                   (
                       m_pane = new MyPane()
                   )
                   .build();
              for (int i=0; i<3; i++)
              {
                  Button b = new Button("Hello " + i);
                  m_pane.getChildren().add(b);
              }
              sysoutNodePrefSizes("------ having created and added the buttons");
              primaryStage.setScene(scene);
              sysoutNodePrefSizes("------ having assigned the scene");
              primaryStage.show();
              sysoutNodePrefSizes("------ having called stage.show");
          }
          
          private void sysoutNodePrefSizes(String title)
          {
              System.out.println(title);
              for (Node child: m_pane.getChildren())
                  System.out.println(child.prefWidth(-1) +  "/" + child.prefHeight(-1));
          }
      }
      The result in the console is:
      ------ having created and added the buttons
      0.0/0.0
      0.0/0.0
      0.0/0.0
      ------ having assigned the scene
      0.0/0.0
      0.0/0.0
      0.0/0.0
      ----- doing the layout
      58.0/20.9609375
      58.0/20.9609375
      58.0/20.9609375
      ------ having called stage.show
      58.0/20.9609375
      58.0/20.9609375
      58.0/20.9609375
      ----- doing the layout
      58.0/20.9609375
      58.0/20.9609375
      58.0/20.9609375

      Edited by: user10257069 on 09.11.2012 00:48 - reformatted the code...