Flex 2 Containers and the "parent" Property

Did you know that children of a Flex Container may actually be its grandchildren? Containers in Flex have a property called contentPane that is of type mx.core.FlexSprite. If certain conditions are met, such as when the container needs scrollbars, new components may actually get added to this contentPane instead.

I discovered this behavior when I noticed that some components inside a Canvas container were behaving differently than the rest. The typically stopped drawing some of their children or they wouldn’t draw at the correct size. Normally, this wouldn’t happen, but I had quite a bit of code manipulating the display list to improve performance, and I kept running into issues when I used containers. Each child of the Canvas seemed to think that the Canvas was it’s parent, but I wasn’t so sure. I dug into the framework source code, and I discovered a hidden gem in UIComponent:

mx_internal final function get $parent():DisplayObjectContainer

Since components may override the parent property, Adobe engineers added this $parent property just in case. I traced out the value, and that’s when I found contentPane. Adobe’s documentation says it best:

This property allows access to the Player’s native implementation of the ‘parent’ property, which can be useful since components can override ‘parent’ and thereby hide the native implementation. Note that this “base property” is final and cannot be overridden, so you can count on it to reflect what is happening at the player level.

In my project, I might add a child to the Canvas container when it isn’t actually on the display list. I’m guessing that certain initialization code, or maybe even some validation code, never runs when the contentPane is active under these conditions. I’ve started using a subclass of the regular UIComponent instead of a Canvas now, and it seems to work fine. File this under, “Random Stuff I’ll Probably Never Encounter, But Still Good to Know”.

About Josh Tynjala

Josh Tynjala is a frontend developer, open source contributor, bowler hat enthusiast, and karaoke addict. You might be familiar with his project, Feathers UI, an open source user interface library for Starling Framework that is included in the Adobe Gaming SDK.

Discussion