This one was interesting, at first glance looks like it should work, 
until I actually tried it.

Turns out TeatArea inherits from UIComponent, but it does not inherit 
from Container, which contain methods for adding and removing Flex 
children. So you cannot add an Image to a TextArea using addChild(), 
which is a method of the Flash API. It's not perfect, but it's the only 
explanation I can think of why this would not work.

Why don't you set your TextArea backgroundAlpha to 0, wrap your TextArea 
in another Container like Canvas, and add your Image to that container 
over top of the TextArea? If your TextArea is instantiated in MXML, swap 
the depths of the two so your image appears beneath the TextArea. You 
can still get the TextArea to appear to have a white background by 
setting Canvas backgroundAlpha to 1 and the backgroundColor to white. A 
little convoluted, but it gives you more control. And saves you a few 
ulcers ;)

An easier way, of course, is to simply set the backgroundImage style to 
your image, if don't you mind it being centred or stretched.


Consequently, TextArea does inherit addChild() from 
DisplayObjectContainer, but DisplayObjectContainer is a part of the 
Flash API, not the Flex API. So you'd think that adding the asset cast 
as Bitmap or Sprite (without the Image) would work, but it doesn't, I 
guess cause there's no rawChildren property for TextArea. Interesting. 
If anyone's got an explanation why you can't addChild([img asset]) on a 
TextArea I'd like to hear it.


_______________________________________________________________________

Joseph Balderson | http://joeflash.ca
Flex & Flash Platform Developer | Abobe Certified Developer & Trainer
Author, Professional Flex 3 (coming Winter 2008)
Staff Writer, Community MX | http://communitymx.com/author.cfm?cid=4674



aarhac wrote:
> Hello,
> 
> I have figured out how to work around my problem, but I do wonder why
> one of the following methods works, and the other doesn't. I would be
> grateful if someone could explain the error in the failing method.
> 
> The code is pasted below (Below the line of dashes) in it's entirety,
> and is commented as to which works and which doesn't.
> 
> The image referenced in this case is a 32x32 pixel png file on my
> local system. I am attempting to display the image "on" a text area.
> 
> Using the default example from the flex documentation to embed an
> image, I cannot get the image to show up when added to the text area's
> display list, but it does show just fine when added to the root
> display list. I also added the image to the textArea using
> addChildAt(txtIcon, txtArea.numChildren) with no change in effect.
> 
> The second method works just fine. I imagine that the problem has
> something to do with applying the image as a byte array to
> image.source, but I wonder why exactly? Is this a bug?
> 
> Thanks in advance,
> 
> --Aaron
> 
> ------------------------------------
> 
> <?xml version="1.0" encoding="utf-8"?>
> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml";
> layout="absolute">
>       
> <!--Works-->
> <mx:Image id="txtIcon" source="@Embed(source='c:/icon.png')"/>
> 
> <mx:TextArea id="txtArea" width="400" height="200" click="addImage()"/>
> 
> <mx:Script>
>     <![CDATA[
>         import mx.controls.Image;
>         import mx.core.BitmapAsset;
>         
>         // Doesn't work
>         [Embed(source="c:/icon.png")]
>         [Bindable]
>         public var imgCls:Class;
> 
>         public function addImage():void
>         {
>             // Doesn't work
>             var loadedImg:Image = new Image();
>             loadedImg.source =  new imgCls() as BitmapAsset;
> 
>             loadedImg.x = 10;
>             loadedImg.y = 10;
>             txtArea.addChild(loadedImg);
> 
>             // Note: While the above doesn't work, adding the 
>             // image to the root display list does work:
>             //addChild(loadedImg);
> 
>             // Works
>             txtIcon.x = 50;
>             txtIcon.y = 10;
>             txtArea.addChild(txtIcon);
>         }
>     ]]>
> </mx:Script>
> 
> </mx:Application>
> 
> 
> 
> ------------------------------------
> 
> --
> Flexcoders Mailing List
> FAQ: http://groups.yahoo.com/group/flexcoders/files/flexcodersFAQ.txt
> Search Archives: 
> http://www.mail-archive.com/flexcoders%40yahoogroups.comYahoo! Groups Links
> 
> 
> 
> 

Reply via email to