Reputation: 9016
I used this code on Flex 3.5 SDK with Flash Builder 4 inside a function that is being called on creationComplete of the MXML app.
var myButton:Button = new Button;
myButton.label = "test";
addChild(myButton);
Alert.show("Button Created");
It works fine however, when I use it on the same Flash Builder 4 only this time, under Flex 4.0 SDK, nothing's happening. The Alert.show() isn't even showing which means it doesn't even get to that point.
So my question is, what's wrong? Am I missing something?
P.S. I need to be able to create / remove MXML components on the fly (while the app is running). This is just a test script and I'm failing miserably at achieving what I need.
Upvotes: 1
Views: 1537
Reputation: 61
Usually you create the UI in MXML, but in some cases you do need to create UI elements on the fly, and as the other poster mentioned, addElement() is the key with Flex 4 Spark containers.
When adding components to MX containers (from Flex 3), you still use addChild() in Flex 4. You only need to use addElement() when adding to Spark containers.
Of course, Adobe recommends you use the Spark containers when there is a somewhat comparable MX container.
Upvotes: 1
Reputation: 325
In Spark you need to use addElement instead of addChild
var b:Button = new Button();
addElement(b);
I'm not sure why your alert isn't working :\
Upvotes: 1
Reputation:
Its better that you do it in the flex way.
<fx:Script>
<![CDATA[
import mx.controls.Alert;
private function alert():void
{
Alert.show("Button added to stage");
}
]]>
</fx:Script>
<s:Button id="myButton" label="test" addedToStage="alert()"/>
You can't add a button using mere addChild in flex. First you need to create an UIComponent and then add the button to the UIComponent. Its a bit different from the flash way.
Upvotes: 0