John
John

Reputation: 13699

Dynamically creating a CheckBox with ActionScript

Here's what I have so far,

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
               creationComplete="init()">
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>

    <fx:Script>
        <![CDATA[
            import spark.components.CheckBox;

            private function init():void
            {
                var _cb:spark.components.CheckBox= new spark.components.CheckBox();
                _cb.name = "alsowhatever";
                _cb.y = 40;
                addChild(_cb);

            }
        ]]>
    </fx:Script>

</s:Application>

the page does not display the CheckBox as expected, could someone point out where I have gone wrong. (Flash Builder lists my version of Flex as 4.1)

Upvotes: 1

Views: 1101

Answers (3)

Neil
Neil

Reputation: 11

You have _cb.name It should read ad follows:

            var _cb:spark.components.CheckBox= new spark.components.CheckBox();
            _cb.label = "alsowhatever";
            _cb.y = 40;
            addElement(_cb);

Neil

Upvotes: 1

fonini
fonini

Reputation: 3341

You must use addElement() instead of addChild().

Upvotes: 1

RIAstar
RIAstar

Reputation: 11912

That's a Spark Application: you should use addElement() instead of addChild(). Use addChild() in mx components only.

Admitted: it's somewhat confusing. Why is there still the public function addChild() if I cannot use it? Well that's because all components extend UIComponent (including the Spark components). So addChild() is only still there for legacy reasons.

Upvotes: 1

Related Questions