View Full Version : Trouble with adding components to VBox
Danenania
04-10-2008, 08:49 AM
Hello all. I'm making my first Flex 3 application in FlexBuilder and I'm having trouble adding components to a VBox with ActionScript during runtime. The Vbox is the child of a ViewStack if it makes any difference.
Am I doing something wrong in this code? Everything compiles and executes until the last line of what I've quoted below where it stops, and no button is displayed.
var testbutton:Button = new Button;
testbutton.height = 30;
testbutton.label = "Hi";
StacksLblVbox.addChild(testbutton);
Of course I really want to do something more complex than this but I gotta figure out this first. Thanks for your help!!
gghazaryan
04-10-2008, 08:55 AM
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" applicationComplete="complete(event)">
<mx:Script>
<![CDATA[
import mx.controls.Button;
import flash.net.getClassByAlias;
private function complete(evt:Event):void{
var testbutton:Button = new Button();
testbutton.height = 30;
testbutton.label = "Hi";
xxx.addChild(testbutton);
}
]]>
</mx:Script>
<mx:Image x="266" y="199" id="abc"/>
<mx:VBox id="xxx"/>
</mx:Application>
This code works fine.
I think you put your code in wrong event handler...
kahuja
04-10-2008, 01:17 PM
Can you share you code, so that we can see what is wrong, better than guessing.
Danenania
04-10-2008, 08:45 PM
Thanks for the replies! My application is too big to post everything, but I'll post the relevant stuff so you can follow it better.
For background, it's a poker related application and the user needs to input whether to show the stack and then the stack size for each player, so the number of inputs added depends on the number of players the user has selected for the game in a previous menu. What I really want to do is populate 3 Vboxes one with text areas that act as multiline labels, one with check boxes, and one with text inputs. I want to add these to each Vbox in arrays so I can iterate through them. Here's a SS so you can see what I mean. The top line of components is the same no matter how many players there are, so I made it permanent so I'd have a model for the properties of the components added at runtime.
http://img403.imageshack.us/img403/7674/stacksselectssza6.png
Now here's the mxml code:
</mx:Canvas>
<mx:Canvas label="Stackscnv" width="100%" height="100%" fontSize="12" id="Stackscnv">
<mx:Text x="0" y="0" text="3" height="57" fontFamily="Georgia" fontWeight="bold" fontSize="45" color="#1D41DB" paddingTop="0" paddingBottom="0"/>
<mx:Label x="35" y="29" text="Stacks:" fontSize="12" color="#1D41DB"/>
<mx:VBox x="0" y="136" width="43" height="376" verticalGap="1" id="StacksLblVbox">
<mx:TextArea width="42" wordWrap="true" borderStyle="none" backgroundAlpha="0.0" height="36" text="SB:" id="StacksSBlbl" editable="false" selectable="false"/>
</mx:VBox>
<mx:Label x="51" y="115" text="Show?" textDecoration="underline" id="StacksChkVbox"/>
<mx:Label x="113" y="115" text="Stack" textDecoration="underline" id="StackTxtVbox"/>
<mx:RadioButton x="10" y="58" label="In $" id="StacksBBDollars"/>
<mx:RadioButton x="63" y="58" label="In Big Bets" id="StacksBBradio"/>
<mx:Button x="5" y="91" label="Show All" width="65" height="16" fontSize="12" paddingBottom="1" paddingLeft="1" paddingRight="1" fontWeight="normal" paddingTop="0" id="StacksShowAll"/>
<mx:Button x="78" y="91" label="Show None" width="75" height="16" fontSize="12" paddingBottom="1" paddingLeft="1" paddingRight="1" fontWeight="normal" paddingTop="0" id="StacksShowNone"/>
<mx:VBox x="51" y="136" width="40" height="376" horizontalAlign="center" verticalGap="20" id="StacksChkVbox">
<mx:CheckBox textAlign="center" id="StacksSBchk"/>
</mx:VBox>
<mx:VBox x="95" y="136" width="61" height="376" id="StackstxtVbox">
<mx:TextInput width="100%" fontSize="10" paddingLeft="0" paddingRight="0" maxChars="9" id="StacksSBtxt"/>
</mx:VBox>
</mx:Canvas>
I want this to happen when the Next button of the previous form is clicked, here's the mxml for the button that calls the function:
<mx:Button x="77" y="296" label="Next" height="21" fontSize="12" color="#1D41DB" fontWeight="normal" id="CH2Next" click="ch2nextclick()"/>
The AS code is stored in a separate file. I include it like so at the top of the mxml.
<mx:Script source="PMController/CreateHand.as"/>
And finally here's my AS code:
import mx.controls.TextArea;
import mx.controls.TextInput;
import mx.controls.CheckBox;
private var positions:Array
private var stackslbl:Array
private var stackschk:Array
private var stackstxt:Array
private function ch2nextclick():void{
CreateHandVS.selectedChild = Stackscnv;
fillstakesselect();
}
private function fillstakesselect():void{
//Fills the stacks form
//Instantiate arrays for each component type
stackslbl = new Array;
stackschk = new Array;
stackstxt = new Array;
/*Loop through from 1 to the number of players - 1 adding rows of components (not from 0 because of the permanent row of components)*/
for(var i:int = 1; i <= pmactionnav.pmdealer.pmtable.noplayers 1; i++){
stackslbl[i] = new TextArea;
stackslbl[i] = StacksSBlbl;
stackslbl[i].text = positions[i]; /*positions is an array that was populated with strings for each player at an earlier point.*/
StacksLblVbox.addChild(stackslbl[i]);
stackschk[i] = new CheckBox;
stackschk[i] = StacksSBchk;
StacksChkVbox.addChild(stackschk[i]);
stackstxt[i] = new TextInput
stackstxt[i] = StacksSBtxt
StackstxtVbox.addChild(stackstxt[i])
}
}
EndlessLine
04-10-2008, 10:53 PM
I think the problem is how your including your external actionscript. I don't think you can do it with the source parameter of the Script tag. I believe you will need to use import like so... (just a quick paste from something i'm working on) hope it helps!
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="init()">
<mx:Script>
<![CDATA[
import actions.KPacc.KPacc;
import actions.forms.EmailForm;
public function init():void{
var myAcc:KPacc = new KPacc;
var myForm:EmailForm = new EmailForm;
myAcc.createAccord(testCV);
}
]]>
</mx:Script>
<mx:Panel id="testCV">
</mx:Panel>
</mx:Application>
|
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.