start flex with a simple and basic asignment
Posts in category Flex
start flex with a simple and basic asignment
Using JSON with Flex 2 and ActionScript 3
JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition – December 1999. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language.
In a nutshell, JSON is a way to serialize data using JavaScript syntax. It is fairly compact, and easy to use. Furthermore, it has become increasingly popular due to the growth of AJAX applications, and the browser’s native support of de-serializing JSON (via eval()).
If you look on the main page of my weblog, the Reading / Doing section on the left is actually a small AJAX widget I created that shows my latests diggs, delicious bookmarks, and items added to my ta-da lists. It gets its data from a JSON feed on the server, which is periodically generated by a PHP file.
So, to show how to use JSON within a Flex / Flash application, we will load the JSON feed from my site into Flash and display it in a datagrid. Nothing earth shattering, but it will show how easy it is to use JSON. I am not assuming that you know anything about Flex / ActionScript or JSON. Because of this, the tutorial will be pretty long, as I want to make sure I cover every step of the process.
You can view the completed example here. (Requires Flash Player 8.5 beta 2).
First, you need to make sure you have the following installed:
You can grab Flex Builder and MXMLC from labs (the MXMLC compiler is included with the Flex Builder install, and also in the SDK download (in case you are on MAC or Linux).
Once you download the zip file for the corelib library, unzip it and you should see three subfolders: src, bins, docs.
- src : contains the source code for the library
- bin : contains a compiled SWC of the library (you can think of this as a DLL)
- docs : contains the API docs for the library
You can link the project against either the source, or the SWC. For this example, we will use the SWC since we don’t need to edit the source code any.
Once Flex Builder is install, open it and make sure that you are in the Flex Development perspective.
Window > Open Perspective > Flex Development
Next, make sure the navigator view is open (it is usually on the left). This view shows all of your projects and files. If the view is not open, you can open it via:
Window > Show View > Navigator
Right click in the navigator and select:
New > Flex Project
Name the Project “JSONExample” and make sure “Use default location” is checked.
Then, click the “Next” button (not finish), this takes us to a screen that will allow us to add items to our class path, or to link against libraries. (you can access this at anytime, by selecting the project, and then selecting Project > Properties).
Select the Libraries tab, and click Add SWC. Browser to the corelib.swc file which was including in the bin directory of the corelib zip file. Click the Finish button.
The first thing we need to do is to add a datagrid to display the data. Our grid will contain two columns. One for the title, and one for the type of service (delicious, digg, tada).
Make sure that the JSONExample.mxml file is open in the editor (double click it in the Navigator), and then switch to design view (click the Design button in the top left of the editor). This brings us to design view which allows us to visually layout and manipulate our components.
Once you are in design view, you should see a Components view. If not, you can open it via Window > Show View > Components. This view contains all of the built in (and any custom components) that are available to the project. Select a DataGrid component from the Controls folder and drag it onto the stage. Once on the stage, resize it so that it takes up most of the space. The grid should snap to the edges when it gets close.
Make sure the grid is selected then go to the Flex Properties view. It should be open on the right, but if it isn’t you can open it via Window > Show Views > Flex Properties. We need to do two things here:
- Give the control an ID
- Set the layout constrains
Select the Id field at the top of the Flex Properties panel, and enter “grid”. This basically gives the grid a variable name so that other controls and ActionScript can reference it.
Next, scroll all the way to the bottom of the Flex Properties view to the Layout section. This will show your component with anchor points. Make sure that the following check boxes are checked:
- Top Right
- Top Left
- Left Top
- Left Bottom
This basically tells the controls to keeps its sides the same distance from the application’s sides, even if the app / windows resizes.
At this point, we are ready to switch back to source view, and load the JSON data. So, switch back to source view (click the Source button in the top left of the editor).
You code should now look something like this:
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns="*" layout="absolute"> <mx:DataGrid id="grid" right="10" left="10" top="10" bottom="10"> <mx:columns> <mx:DataGridColumn headerText="Column 1" dataField="col1"/> <mx:DataGridColumn headerText="Column 2" dataField="col2"/> <mx:DataGridColumn headerText="Column 3" dataField="col3"/> </mx:columns> </mx:DataGrid> </mx:Application>
Lets add an HTTPService tag to actually load the data. Add the following tag right under the Application tag.
<mx:HTTPService id="service" resultFormat="text" url="http://weblogs.macromedia.com/mesh/mashedpotato.json" result="onJSONLoad(event)" />
If you save this, you should get an error in the problems view (Window > Show Views > Problems):
Call to a possibly undefined method 'onJSONLoad'
This is ok. We just need to define the onJSONLoad event handler. But first, lets look at the code we just wrote:
- id – Give the control a variable name so we can reference it later.
- url – the url that points to the JSON data we are loading
- resultFormat – the format that we want the data returned to us in. (In this case, just the raw text).
- result – event handler that is called when the data loads.
At this point, your code should look something like this:
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns="*" layout="absolute"> <mx:HTTPService id="service" resultFormat="text" url="http://weblogs.macromedia.com/mesh/mashedpotato.json" result="onJSONLoad(event)" /> <mx:DataGrid id="grid" right="10" left="10" top="10" bottom="10"> <mx:columns> <mx:DataGridColumn headerText="Column 1" dataField="col1"/> <mx:DataGridColumn headerText="Column 2" dataField="col2"/> <mx:DataGridColumn headerText="Column 3" dataField="col3"/> </mx:columns> </mx:DataGrid> </mx:Application>
Next, we need to create a script block to define our event handler function. Add a Script tag block just below the Application tag:
<mx:Script> <![CDATA[ ]]> </mx:Script>
and then inside the Script block, lets define our event handler. You script block should now look like this:
<mx:Script>
<![CDATA[
import mx.rpc.events.ResultEvent;
private function onJSONLoad(event:ResultEvent):void
{
}
]]>
</mx:Script>
If you look at the API docs, ResultEvent is the type of event broadcast when the data has loaded from the service.
Next, all we need to do is:
- Get the raw JSON data from the URL
- De-serialize it from JSON to ActionScript
- Set the data as the dataProvider for the grid
Here is the completed function, with comments:
Best Adobe Flex Resources and Tutorial Sites
Considering that this website is based around creating easy to follow and useful tutorials, I decided to share the places I go for Flex tutorials and information. Now of course the number one place I would go to find Flex tutorials would be this site, but you already knew that.
My first list here is some of the great places I go to find Flex tutorials. If you can’t find a particular item at one of these sites it’s going to be hard to find it anywhere on the web. These are in no particular order but I was very picky on what I put in this category. I apologize in advance to anyone who feels I have missed a big site or blog.

Flex Examples
http://blog.flexexamples.com/
Peter deHaan currently works for Adobe on the Flex SDK QA team. He writes so many small tutorials that it’s hard to even keep up with this man on any level. I always go to this site if I am in need of a small specific item.

Dzone
http://www.dzone.com/links/index.html
Dzone is a great community based site for sharing developer links. The site is setup in a similar fashion as popular sites such as Digg or Reddit. The great thing is that this site has as many tutorials linked on it as anywhere else on the web.

Adobe Flex Developer Center and Cookbook
http://www.adobe.com/devnet/flex/
The Adobe Developer Center is great resource for complete tutorials and articles on how to get started with Flex or any of the other Adobe products. The other Adobe resource is the Cookbook which is built off of community code snippets that solve small programming tasks.

Doug McCune
http://dougmccune.com/blog/
Doug is a pretty big name in the Flex community (he gives presentations all over the country at Flex events), most of the items you are going to find on his site are examples of cool applications built using Flex. These examples usually include the source code. I would not strictly call them tutorials, but all the source and examples make it a great place to learn Flexing techniques.

http://www.google.com
Sounds cliche but Google is the best single source for finding Flex tutorials and information. Simply search for anything your looking for (like “flex tutorials”), Google is also the best way find anything in the Flex documentation. Usually I will search for something like “flex 3 combobox” or similar.
The above list is strictly where I go to find and research Flex tutorials. I felt that it would really be a disservice to fail to mention some of the the other great Flex resources out there. The people and places below are some of the best Flex assets out on the web; be sure to take advantage of these.

Franto.com Flash Blog
http://www.franto.com/blog2/
This blog is written by Frantisek Kormanak (Franto) and has great deal of posts on how to program in Flex and in Flash. I would say that most of his posts are informational and include great examples.

Flex.org
http://flex.org/
Flex.org is the main community site for Flex developers, which includes a showcase and resources for everything from PHP to .Net.

Ryan Stewart
http://blog.digitalbackcountry.com/
Ryan Stewart is a Platform Evangelist for Adobe who is also a prolific blogger. He writes about anything involving RIAs and web technologies that catch his fancy. You can also check out his ZDNet blog here.

Alex Harui
http://blogs.adobe.com/aharui/
Alex writes short posts that are usually nice Flex examples solving common and uncommon problems people run across in Flex. He works with the Adobe Flex core team in San Francisco.

Eric Feminella
http://www.ericfeminella.com/blog/
This gentleman writes very interesting pieces on Flex, AS3, and programming architecture. He has given a lot of code to the community and is always trying to architect a new solutions for common issues.
There were also some close runner ups, most of the below people and sites post good information, but just may not post as often or sometimes they are off topic – not that off topic posts are bad.
All things considered I think I have put together a good set of resources for someone who is looking for a myriad of Flex information. If we have missed any site out there let us know in the comments, the more the merrier.
Beginning Adobe Flex Tutorial: Your first Flex Program Part I
Once you are in design view, you should see a Components view. If not, you can open it via Window > Show View > Components. This view contains all of the built in (and any custom components) that are available to the project. Select a DataGrid component from the Controls folder and drag it onto the stage. Once on the stage, resize it so that it takes up most of the space. The grid should snap to the edges when it gets close.
Flex 101 with Flash Builder 4
The article is intended towards developers who have never used Flex before and would like to exercise a “Hello World” kind of tutorial. The article does not aim to cover Flex and FB4 in detail but rather focuses on the mechanics of FB4 and getting an application running with minimal effort.
For developers familiar with Flex and the predecessor to Flash Builder 4 (Flex Builder 2 or 3), it contains an introduction to FB4 and some differences in the way you go about building Flex Applications using FB4.
Even if you have not programmed before and are looking at understanding how to make a start in developing applications, this would serve as a good start.
The Flex Ecosystem
The Flex ecosystem is a set of libraries, tools, languages and a deployment runtime that provides an end-to-end framework for designing, developing and deploying RIAs. All these together are being branded as a part of the Flash platform.
In its latest release, Flex 4, special efforts have been put in to address the designer to developer workflow by letting graphic designers address layout, skinning, effects and general look and feel of your application and then the developers taking over to address the application logic, events, etc.
To understand this at a high level, take a look at the diagram shown below. This is a very simplified diagram and the intention is to project a 10,000 ft view of the development, compilation and execution process.
Let us understand the diagram now:
1. The developer will typically work in the Flash Builder Application. Flash Builder is the Integrated Development Environment (IDE) that provides an environment for coding, compiling, running / debugging your Flex based applications.
2. Your Flex Application will typically consist of MXML and ActionScript code. ActionScript is an ECMAScript compatible Object Oriented language, whereas MXML is an XML-based markup language.
3. Using MXML you can define/layout your visual components like buttons, combobox, data grids, and others. Your application logic will be typically coded inside ActionScript classes/methods.
4. While coding your Flex Application, you will make use of the Flex framework classes that provide most of the core functionality. Additional libraries like Flex Charting libraries and 3rd party components can be used in your application too.
5. Flash Builder compiles all of this into object byte code that can be executed inside the Flash Player. Flash Player is the runtime host that executes your application.
This is high level introduction to the ecosystem and as we work through the samples later on in the article, things will start falling into place.
Flash Builder 4
Flash Builder is the new name for the development IDE previously known as Flex Builder. The latest release is 4 and it is currently in public beta.
Flash Builder 4 is based on the Eclipse IDE, so if you are familiar with Eclipse based tools, you will be able to navigate your way quite easily. Flash Builder 4 like Flex Builder 3 previously is a commercial product and you need to purchase a development license. FB4 currently is in public beta and is available as a 30-day evaluation.
Through the rest of the article, we will make use of FB4 and will be focused completely on that to build and run the sample applications. Let us now take a look at setting up FB4.
Setting up your Development Environment
To setup Flash Builder 4, follows these steps:
1. The first step should be installing Flash Player 10 on your system. We will be developing with the Flex 4 SDK that comes along with Flash Builder 4 and it requires Flash Player 10. You can download the latest version of Flash Player from here: http://www.adobe.com/products/flashplayer/
2. Download Flash Builder 4 Public Beta from http://labs.adobe.com/technologies/flashbuilder4/.
Top 5 Reasons to Choose Adobe Flex
Are you still struggling to find out which RIA solution is best for you? With so many frameworks to choose – AJAX, Adobe Flash, Adobe Flex, Microsoft SilverLight, OpenLaszlo, Curl, JavaFX, do you feel lost somehow?
You have seen all kinds of comparisons or reviews online, such as:
- AJAX vs. Flex vs. SilverLight
- 10 reasons to love AJAX (or Flex, or SilverLight, or …)
- 10 reasons to hate AJAX (or Flex, or SilverLight, or …)
At the end of the day, you may wonder: “Now What?”
The good news is, life can be much simpler! You are not here to select the best movie for OSCAR. Your job is to simply find the most suitable RIA framework for YOU!
As an example, I’d like to share with you why Flex is good for me. My top 5 reasons are:
1. Easy to Learn and Use
Before Flex, I was a Java developer. Before Java, I was a C++ and COBOL guy on IBM Mainframe. So I know exactly how painful it is to learn a new programming language. Just like switching from a manual transmission to an automatic one, it takes time for you to get used to that.
Surprisingly, the first time I saw Flex code, I thought it was Java. Considering the similar syntax, structure, and style, it is like the same coffee with different cream. With no time, I built my first fully functional application in Flex and Wow my colleagues.
Another beauty of Flex is its development tool – Flex Builder 3. It is built on the same platform – Eclipse as my J2EE IDE. So I don’t have to learn a new set of settings, shortcuts, or tricks. Plus Flex Builder 3 provides many useful features, such as design editor, build tools, integrated debugger. It makes your job so much easier! You can get your copy of Flex Builder 3 here.
2. Mature Language and Framework
Unlike JavaScript Based AJAX, Adobe Flex is built on a strongly typed Objected-Oriented programming language. I believe the Flex development team extends a lot of goodies from Java, making it suitable for developing enterprise level applications.
In addition,there are some mature frameworks to support Flex at the architecture level. Like Struts and Spring for Java, command-driven Cairngorm and event-driven Mate are two frameworks to implement the MVC (Model-View-Control) design pattern for Flex. And they both are open sourced projects.
3. Cross Browser Support
How many times do you have to deal with different browsers (IE 6, IE 7, Firefox, Google Chrome, Opera, Safari …) just to make you web pages look right? JavaScript error, PNG image missing, layout broken, you are adding all kinds of patches or hacks to put down the fire. As a result, your code is messy and hard to maintain, you are frustrated and exhausted, but the new problems keep popping up…
Now I have Adobe Flex. With its runtime (Flash Player) having 98% market penetration and working consistently across different browsers, I finally can focus on building a great RIA instead of worrying about its compatibility.
4. Fast Development and Easy Integration
Time is money! It is especially true on Internet. Why is .Net taking much share from Java? One reason is that it provides a lot of build-in modules to fasten the development process. Like .Net, Adobe Flex was born with many convenient components (such us DataGrid, ProgressBar, RichTextEditor, Slider, Accordion, Charts …) to help create a RIA faster and easier. I worked on a Flex project originally designed in Java. It was estimated as 6 months work, but finished within 4 months.
Moreover, Flex might be the best framework to easily integrate with different external systems. It provides components such as HTTPService, WebService, and RemoteObject, plus optional Data Service like LCDS or BlazeDS. In one of my Flex projects, I collected data from J2EE Server, SAP, and 2 other legacy systems to compose the final reports. Without Flex, my job would have been much more complicated.
5. Richest Visual Effect
Yes, RIA is supposed to be cool! The most amazing RIAs can maximize your sensation to nearly feel the target products, like BMW X5 or Sony HDTV. You know what? Most of those RIAs are done by Adobe Flash. Since Adobe Flex is built on top of Flash, it can access all of Flash’s rich libraries and assets. Not mention Adobe is working diligently to bind Flash and Flex seamlessly. Therefore, it is no doubt that Adobe Flex provides you everything you need to build a best-of-all RIA.
In Conclusion
RIA means not only giving a face-lift to your business but also providing a cutting edge over your competitors. When coming to the best RIA framework, different people might give you different options. But the most important is, you evaluate them based on your resources, systems, and requirements. As an example, I put my top 5 reasons to choose Adobe Flex:
How to Choose the Best Adobe Flex Book for You?
Do you struggle to find out which Adobe Flex book is best for you? You’re not alone. Considering there are so many books in the market, such as Flex Bible, Flex Cookbook, and Flex for Dummies, it is hard to make your mind. The bottom line is, you don’t want to buy a book only dusting on your bookcase later. So in this post, I like to share with you five ways to pick up a right Adobe Flex book.
1. What do You Want
Just like a good tool is to help you do a specific job, a good book is to help you resolve your problem. You want to get the comprehensive understanding of Adobe Flex? You want to know how to call Java programs in Flex? Or you want to find out the popular frameworks of Flex? Each book may focus on specific areas. So your first job is to know what you want. Then you try to match it by reading the table of contents of the book.
2. Publish Date
Due to the fast changes of computer technologies, most of programming books have a short life span. Generally speaking, any computer books older than two years are obsolete. Considering the writing process usually takes about six months, you’d better select a book published within a year. For example, it is now August 2009, the ideal Flex book should be published after August 2008.
3. Authors
This might be the most important factor. Since we are using the same Flex SDK from Adobe, why don’t you just stick to Adobe’s official documents? Why do you need an extra Flex book? Because you want to know the unique insight, experience, and tips from the experts in different areas. And you want to see how they use the Flex technology to resolve the real life problems. Therefore, who is the author and what kind of background he has are even more important than the topics. Moreover, I especially like the book written by a group of people. It is mainly because, first you get more in-depth knowledge from gurus specialized in various topics; second, you usually get better quality of contents through the team collaboration.
4. Customer Reviews
Amazon.com is the best place to find out the related information of a book before you buy it. Except for the publish date and authors, I would pay particular attention to customer reviews. Usually, the more reviews a book gets, the better it is. Because it indicates that more people are interested in this book and willing to spend time giving their feedback. Moreover, if you have any specific needs or questions, you might get some ideas how this book will help you by reading those reviews.
5. Add-on Resources and Support
A good Flex book might be your guide to a new world. You’ll not only learn from the book but also get more values from add-on resources and the support. For example, it often comes with a website for you to download sample codes, post your questions, and connect with the authors. Remember the computer technologies are constantly progressing. So you may want to catch up with the experts and get the latest update.
What is My Topic Pick for Adobe Flex Book?
If you just want to get one Best Flex Book, I would highly recommend Professional Adobe Flex 3.
- Give you a comprehensive while in depth understanding of the complete Adobe Flex landscape including: What is Flex, Why Flex, Flex Development Ecosystem, Components Usage and Customization, Data Management, Visual Effect & Multimedia, Client Communication, Server Integration, Data Service, Flex Framework, Development Strategies, Testing and Debugging.
- Published on June 2, 2009 (the newest Flex book you can get now) by Wrox (famous for its Programmer to Programmer philosophy).
- Written by a group of Flex gurus speciaized in various fields. e.g. Joseph Balderson (Seniro Flex and Flash Platform Consultant), Peter Ent (Computer Scientist in Adobe LiveCycle team), Tom Sugden (Technical Architect for Adobe Professional Services), Andrew Trice (Principal Flex and AIR Architect), and David Hassoun (Founder of RealEyes Media).
- Great additional resources and support on wrox.com including sample codes, online library, latest update, and interaction with community members.