[flexcoders] Re: createChildren(): adding 20 identical buttons to panel?

2008-10-22 Thread valdhor
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.




RE: [flexcoders] Re: createChildren(): adding 20 identical buttons to panel?

2008-10-22 Thread Tracy Spratt
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.


 



[flexcoders] Re: createChildren(): adding 20 identical buttons to panel?

2008-10-22 Thread Mic
Thanks for all the help  I was trying to

b1 = new Button;
this.addChild(b1);
this.addChild(b1);
this.addChild(b1); etc which is why it appeared that b1 was all used
up :-)When adding to the displaylist why does  b1 = new Button; have
to be done each time? Why can't b1, which is an instance of the button
class, be duplicated into the list? b1 is just a variable that refers
to the instance, right? Not a unique id. Not sure why this is
different from

mx:Panel
   mx:Button/
   mx:Button/
   mx:Button/
/mx:Panel

TIA, Mic.



Re: [flexcoders] Re: createChildren(): adding 20 identical buttons to panel?

2008-10-22 Thread Paul Andrews

- Original Message - 
From: Mic [EMAIL PROTECTED]
To: flexcoders@yahoogroups.com
Sent: Wednesday, October 22, 2008 10:28 PM
Subject: [flexcoders] Re: createChildren(): adding 20 identical buttons to 
panel?


 Thanks for all the help  I was trying to

 b1 = new Button;
 this.addChild(b1);
 this.addChild(b1);
 this.addChild(b1); etc which is why it appeared that b1 was all used
 up :-)When adding to the displaylist why does  b1 = new Button; have
 to be done each time? Why can't b1, which is an instance of the button
 class, be duplicated into the list? b1 is just a variable that refers
 to the instance, right?

Right, but b1 refers to one button. If you add it ten times over you're 
trying to add the same button to the container not ten different buttons. 
Using new Button() gives you a new button, so writing

b1 = new Button();
this.addChild(b1);

creates a new Button, points b1 at it and adds it to the display list. Do it 
again and then b1 points to a different Button..

The mxml tag notation not only refers to the Button class but also 
instantiates a seprate instance, so you would get ten different buttons.

Paul

  Not a unique id. Not sure why this is
 different from

 mx:Panel
   mx:Button/
   mx:Button/
   mx:Button/
 /mx:Panel

 TIA, Mic.