Skip to main content
March 25, 2009
Answered

Adding Flex Components to Controllers

  • March 25, 2009
  • 1 reply
  • 910 views
Newbie question: Why do I get a type coercion error when using an Box,HBox or VBox as my TextFlow container? I can use Sprite or UIComponent, but if I use anything that inherits from UIComponent I get the type coercion error saying that it doesn't implement IUIComponent.

Next question: Is it unwise to want to use the various types of Flex containers as a container for TextFlow? If so, why?
This topic has been closed for replies.
Correct answer robin_briggs
I am guessing that what you are seeing is an error that happens because with the UIComponent as the TextFlow container, TLF is calling addChild on the UIComponent, which expects to have UIComponents for its children. If you want to add an ordinary DisplayObject, such as a TextLine, to a UIComponent, you must call rawChildren.addChild instead. But TLF doesn't know about UIComponent, and when it adds in the TextLines, it will call addChild. This is likely causing the UIComponent to complain that the TextLine being added is not a UIComponent.

You could try creating a Sprite object, and adding it as a raw child (see rawChildren attribute) of the Box, and then making the Sprite the TextFlow container. Then when TLF adds the TextLines, they will become children of the Sprite. I have had relatively little experience of this, so I can't comment on how well it will work or play in the Flex layout model. You will have to experiment.

Another alternative to consider is to use the new Flex Gumbo components, which are built on top of TLF.

1 reply

robin_briggsCorrect answer
Adobe Employee
March 25, 2009
I am guessing that what you are seeing is an error that happens because with the UIComponent as the TextFlow container, TLF is calling addChild on the UIComponent, which expects to have UIComponents for its children. If you want to add an ordinary DisplayObject, such as a TextLine, to a UIComponent, you must call rawChildren.addChild instead. But TLF doesn't know about UIComponent, and when it adds in the TextLines, it will call addChild. This is likely causing the UIComponent to complain that the TextLine being added is not a UIComponent.

You could try creating a Sprite object, and adding it as a raw child (see rawChildren attribute) of the Box, and then making the Sprite the TextFlow container. Then when TLF adds the TextLines, they will become children of the Sprite. I have had relatively little experience of this, so I can't comment on how well it will work or play in the Flex layout model. You will have to experiment.

Another alternative to consider is to use the new Flex Gumbo components, which are built on top of TLF.
March 26, 2009
Yes, Robin is right-on with this. It's actually not UIComponent, but the Container class, I think, that adds the requirement for UIComponent-only children. You can actually use a plain UIComponent as the container for text flows. TLF works very well in Flex, and there are a lot of different options for handling measuring, layout, etc. I would start with the the TextView class in the Flex 4 SDK to start to get some ideas about how you could write your own flex components that use TLF.

--Brent