How to dynamically add controls in Adobe AIR or Flex?

By | February 14, 2011

Below example shows how to add a label control dynamically in AIR or FLEX.

var L : Label = new Label();
L.addEventListener(MouseEvent.CLICK,LabelListener);
L.name = “my_label_name”;
L.text = “my Text”;
addChild(L);

// This function listens to the mouse click in the above added label.
private function LabelListener(event:MouseEvent):void
{
}

Please note that you can add any control by this method.
You can also add any controls inside a container like VBox by this method.
The above code works for only Flex SDK 3 for working in Flash builder or SDK 4 or above you need to change the above code like this.

var ui:UIComponent = new UIComponent();
ui.addChild(L);
this.addElement(ui);

Leave a Reply

Your email address will not be published. Required fields are marked *