1 Reply Latest reply: Nov 12, 2012 3:44 PM by 973803 RSS

    notification when layout pass is done


      I come from a Flex 3/4 background and I'm starting my first project in JavaFX 2.2. My main trouble with this new technology is usually finding the corresponding JavaFX component/method/practice for doing something that I'm used of doing in Flex.

      For example, I have a custom control created in FXML (with <fx:root/>) and it's associated controller. My custom control is based on a VBox, which contains a mix of SplitPanes, ScollPanes and StackPanes. In the initialize() method of the controller, I want to compute some values base on the inner component's actual dimensions. For instance, set a SplitPane divider default position based on the ratio of its content's width.

      Now here's the problem:

      In the initialize() method, most of the component's getWidth() methods retuns 0. As I understand, this is because the layout pass hasen't been don yet.
      To correct the problem, I override layoutChildren() in my custom component class and manually call initialize() after the calling super.layoutChildren(). Now the getWidth() method returns the correct size for components that have computed sizes, but still returns 0 for the components on which I have set a prefWidth in their constructor. Again I understand that this is because the layout pass STILL isn't complete (thanks to ScenicView :) ).

      As I browsed through the treads of this forum, I noticed that the best practice is to use the Fluent API and simply bind the property I want to compute to the widthProperty (or the layoutBoundsProperty) of the component in the UI. But this isn't acceptable in my case since I want to compute the property only after the application has started, not each time the application window is resized.

      That said, is their a simple way that my controller can be notified when the custom component has finished it's layout pass?

      Note: In Adobe Flex, I would either listen to the applicationComplete or creationComplete events.

      Thank you all for your help!
        • 1. Re: notification when layout pass is done
          Here's my workaround for now:

          I added a ChangeListener to the layoutBoundsProperty of the one of the component of the SplitPane (the one on which the position of the divider is based on). I suppose that when the layoutBounds changes and are greater than 0, the layout of all the other components inside the SplitPane is finished. Then I use a boolean flag to make sure that I compute the divider position only once, because I know that my listener will be called each time the main window/scene is resized.

          This works for now, but I think it would still be easier to be notified when the internal layout() method has completed, instead of listening to the layoutBoundsProperty of one of the inner components. This would make sure that ALL the component are positions and sized based on either their explicit prefSize or their computed size, and that the getWidth() and getHeight() methods always returns the correct value.

          Thanks again for your time.