Thursday, December 27, 2007

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 :

Sunday, December 23, 2007

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.

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.

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 !

Now that you know the basics of writing Flex applications, you can visit livedocs.adobe.com to learn about the various built-in components.For custom built components however, there are very few tutorials to aid you. I will be talking about adding a FlexBook component to your application. This allows you to layout your components on a set of pages, in a book, and also gives you a nice page-turning effect. This component has become very popular recently, and you can see it on many online magazines.

Create a new project named, say "MyBook" using FlexBuilder. Then, unzip the file you downloaded into the project's main directory. This should create a folder named 'qs' containing all the ActionScript files.

Now, to create your simplest application with FlexBook, use the following code :

This creates a FlexBook for you with a front and back-cover. Put in any container like a HBox or a list within the FlexBook tags and you should be able to see them inside a book when you build and run your application. I am sure this has given you an insight into how to use custom components, and how to integrate them with your project.

Now that you know how to use a custom component, next time I will be teaching you about how to create your own custom component using MXML and ActionScript.

The above program, creates a simple application, with a single button. On clicking the button, you get a "Hello World" alert box shown.The click event handler for the button is the sayHello() function. Much of the code is self-explanative if you know object oriented and event-handling concepts, along with some simple javascript.