Showing posts with label custom components. Show all posts
Showing posts with label custom components. Show all posts

Thursday, December 27, 2007

Creating custom flex components using Actionscript

You already know how easy it is to create your own custom component by extending the functionality of other components (both built-in and custom) using MXML alone. You will now see how to do it, using ActionScript.

Lets name our component, BooleanComboBox. You create this custom component, by extending the ComboBox, which is a built-in component. Your component class must be created inside a package for it to be referenced later on. The code is shown below :

package components
{
import mx.controls.ComboBox;
public class BooleanComboBox extends ComboBox
{
public function CountryComboBox()
{
dataProvider = [ "True", "False" ];
}
}
}
As you can see, in the constructor of this class, we are loading the values "True" and "False" into the dataprovider. As simple as that !

For you to use it in your MXML, there is nothing new to be done. Use it the way you had when you created the component using MXML alone.

<?xml version="1.0"?>
<mx:application mx="http://www.adobe.com/2006/mxml" cc="components.*">
<!-- Use the filename as the MXML tag name. -->
<cc:BooleanComboBox/>
</mx:Application>

So, you now know how you can create your own custom components. Couldn't get simpler than that.
Will talk about communicating with a server in the next post.

Sunday, December 23, 2007

Creating Custom Flex Components Using MXML

Creating custom components allows you to modularize your code, and also make it more reusable. Also, you can extend the functionality of pre-defined components using this concept. So now, lets dive straight into the creation of our own custom component.

Creating custom component involves the use of MXML and some amount of Actionscript too. Now, let us create a ComboBox component that allows the user to choose from True or False. Lets name it BooleanComboBox.

<?xml version="1.0"?>
<mx:ComboBox xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:dataProvider>
<mx:String>TRUE</mx:String>
<mx:String>FALSE</mx:String>
</mx:dataProvider>
</mx:ComboBox>
Save this file as BooleanComboBox.mxml. This completes the creation of your custom component using just MXML. As you can see, creating a custom component is not very different from creating an application. The only difference is, the root tag, is not Application, but its ComboBox here. You can change this to the other components to extend them in a similar manner.

Now, to use it in your application, use the following code,
<?xml version="1.0"?>
<mx:application mx="http://www.adobe.com/2006/mxml" cc="*">
<!-- Use the filename as the MXML tag name. -->
<cc:BooleanComboBox/>
</mx:Application>
Here, you see that, 'cc' is the namespace to reference your custom component. This is defined in the application tag itself. Then, by using the MXML file's name as the tag name, you are using your custom component in your application.

As simple as that :-)
Next, I will be teaching you, how to create your own custom component using ActionScript. So hang in there !