I would advise Repeater for this.  It willsave a lot of code and
provides some other benefits like automatically building the array of
references, and optionally recycling children and handling removal of
the children.

 

Tracy

 

________________________________

From: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] On
Behalf Of valdhor
Sent: Wednesday, October 22, 2008 11:44 AM
To: flexcoders@yahoogroups.com
Subject: [flexcoders] Re: createChildren(): adding 20 identical buttons
to panel?

 

Here is a quick and dirty example on how to add multiple instances as
well as how to access them after creation:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml";
layout="absolute"
    xmlns:custom="CustomClasses.*"
creationComplete="onCreationComplete()">
    <mx:Script>
        <![CDATA[
            import mx.controls.Button;
            
            private function onCreationComplete():void
            {
                var panelChildren:Array = panelTest.getChildren();
                for(var i:int = 0 ; i < panelChildren.length ; i++)
                {
                    if(panelChildren[i] is Button)
                    {
                        trace((panelChildren[i] as Button).label);
                    }
                }
            }
        ]]>
    </mx:Script>
    <custom:PanelTest id="panelTest"/>
</mx:Application>

PanelTest.as:
package CustomClasses
{
    import mx.containers.Panel;
    import mx.controls.Button;

    public class PanelTest extends Panel
    {
        public function PanelTest()
        {
            super();
            for(var i:int = 0 ; i < 20 ; i++)
            {
                var b1:Button = new Button();
                b1.label = "Button " + (i + 1);
                addChild(b1);
            }
        }
    }
}
--- In flexcoders@yahoogroups.com, "Mic" <[EMAIL PROTECTED]> wrote:
>
> public class PanelTest extends Panel{
> private var b1:Button;
> 
> override protected function createChildren():void {
> super.createChildren();
> 
> b1 = new Button;
> this.addChild(b1);
> 
> In order to add another 19 buttons, must vars b2 to b20 be declared
> etc? This is a very simplistic example of what I need to do (
> dynamically add multiple instances of the same complex component
> class to a layout). Because b1 cannot be "reused" for another
> addChild(), how would you add the other buttons? TIA,
> 
> Mic.
>

 

Reply via email to