R – How to add a button to the stage in flex using only as3

actionscript-3apache-flexvisual studio

I am using the Flex SDK within visual studio and trying to dynamically add a button to the stage. Here is a quick example of what I am doing.

public class Test extends Sprite
{                       
    public function Test()
    {
        init();                 
    }                                                               

    private function init():void
    {

        var btnBrowse:Button = new Button();                                        
        btnBrowse.label = "Browse";
        btnBrowse.x = 0;
        btnBrowse.y = 0;
        btnBrowse.width=100;
        btnBrowse.height=100;           
        addChild(btnBrowse);
    }
}

Nothing seems to show up and the screen is still empty. I am importing mx.controls.* for the button. Could that create an issue since I am not using mxml only as3?

Best Answer

You can't use Flex framework controls in an AS3 Only project. If you are trying to avoid MXML then just create a new Flex Project where the root tag is like:

<FooApplication xmlns="*"/>

And create a new AS3 Class like:

package {

import mx.core.Application;

public class FooApplication extends Application {

// now override something like createChildren to add a button.

}
}