The following example shows how you can create a simple, inline custom component in Flex 4 using the Component tag and className property and then instantiating new instances using ActionScript.
Full code after the jump.
The following example(s) require Flash Player 10 and the Adobe Flex 4 SDK. To download the Adobe Flash Builder 4 beta, check out the Adobe Flash Builder 4 page on the Adobe Labs site. To download the latest build of the Flex 4 SDK, see http://opensource.adobe.com/wiki/display/flexsdk/Download+Flex+4. For instructions on using the beta Flex 4 SDK in Flex Builder 3, see "Using the beta Flex 4 SDK in Flex Builder 3".
<?xml version="1.0" encoding="utf-8"?> <!-- http://blog.flexexamples.com/2009/11/05/creating-a-custom-component-using-the-component-tag-in-flex-4/ --> <s:Application name="Spark_Component_className_test" xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark"> <fx:Script> <![CDATA[ import mx.graphics.SolidColor; private function init():void { var c:FancyCircle = new FancyCircle(); c.fill = new SolidColor(Math.random() * 0xFFFFFF /* color */, Math.random() /* alpha */); gr.addElement(c); } ]]> </fx:Script> <fx:Declarations> <fx:Component className="FancyCircle"> <s:Ellipse> <s:stroke> <s:SolidColorStroke /> </s:stroke> </s:Ellipse> </fx:Component> </fx:Declarations> <s:Panel id="pnl" horizontalCenter="0" verticalCenter="0"> <s:controlBarContent> <s:Button id="btn" label="Add Circle" click="init();" /> </s:controlBarContent> <s:Scroller verticalScrollPolicy="on"> <s:Group id="gr"> <s:layout> <s:TileLayout requestedColumnCount="4" requestedRowCount="3" columnWidth="100" rowHeight="100" /> </s:layout> </s:Group> </s:Scroller> </s:Panel> </s:Application>
Or, if you want to create a new instance of your custom component using MXML, you can create a local namespace, as seen in the following example:
<?xml version="1.0" encoding="utf-8"?> <!-- http://blog.flexexamples.com/2009/11/05/creating-a-custom-component-using-the-component-tag-in-flex-4/ --> <s:Application name="Spark_Component_className_test" xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:local="*"> <fx:Declarations> <fx:Component className="FancyCircle"> <s:Ellipse> <s:stroke> <s:SolidColorStroke /> </s:stroke> </s:Ellipse> </fx:Component> </fx:Declarations> <s:Panel id="pnl" horizontalCenter="0" verticalCenter="0"> <s:Scroller verticalScrollPolicy="on"> <s:Group id="gr"> <s:layout> <s:TileLayout requestedColumnCount="4" requestedRowCount="3" columnWidth="100" rowHeight="100" /> </s:layout> <local:FancyCircle> <local:fill> <s:SolidColor color="red" /> </local:fill> </local:FancyCircle> <local:FancyCircle> <local:fill> <s:SolidColor color="haloOrange" /> </local:fill> </local:FancyCircle> <local:FancyCircle> <local:fill> <s:SolidColor color="yellow" /> </local:fill> </local:FancyCircle> <local:FancyCircle> <local:fill> <s:SolidColor color="haloGreen" /> </local:fill> </local:FancyCircle> <local:FancyCircle> <local:fill> <s:SolidColor color="haloBlue" /> </local:fill> </local:FancyCircle> </s:Group> </s:Scroller> </s:Panel> </s:Application>
This entry is based on a beta version of the Flex 4 SDK and therefore is very likely to change as development of the Flex SDK continues. The API can (and will) change causing examples to possibly not compile in newer versions of the Flex 4 SDK.

{ 2 comments… read them below or add one }
“if you want to create a new instance of your custom component using ActionScript,” shouldn’t that be “if you want to create a new instance of your custom component WITHOUT using ActionScript,” ?
@david r,
Good catch, thanks!
Peter