views:

3435

answers:

2

I want to set the icon of a button in flex.

The default syntax is as follows.

myButton.setStyle("icon", iconClass);

and iconClass is normally an embedded object.

But what I want to do is, use a standard Sprite or a MovieClip (which I find during runtime) as the icon.

Is this possible? Has anyone done this?

Thanks!

A: 

Found a possible solution. http://blog.benstucki.net/?p=42 Will give this ago and report.

Chands
+1  A: 

This is actually a fatal flaw in Flex's framework in regards to styling. When Flex grabs a style value for an icon it assumes it's of type Class and that the instantiated object will be of type DisplayObject (or derived). It's a trivial change to the code (which classes like mx:Image do for their source property) to test whether or not the style's value is of type DisplayObject and if so, just skip the construction step that it currently performs.

Ben's solution is the best possible if you won't want to change the Flex framework source. Personally, I end up monkey-patching the specific components to accept instantiated DisplayObjects instead of Classes. Some components are more of a pain than others to patch.

Doug McCune explaining how to monkey patch the Flex framework.

Troy Gilbert