O'Reilly logo

Flex 4 Cookbook by Garth Braithwaite, Joshua Noble, Todd Anderson, Marco Casario, Rich Tretola

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 5. Components

The Flex 4 SDK provides a set of classes and user interface (UI) components to facilitate rapid and standardized development. The fourth iteration of the SDK has been designed to enable the use of Flex 3 (Halo) components as well as Flex 4 (Spark) components based on the new architecture. By default, the Spark and Halo components are differentiated by the s and mx namespaces, respectively. For example:

<s:Button id="sparkButton"/>
<mx:Button id="haloButton"/>

This use of XML namespaces enables developers to switch between the new Spark components and the legacy Halo components. Additionally, it improves the readability of the code. Although many Spark components have Halo counterparts and the two can often be used interchangeably, it is recommended that you use the Spark versions when possible as they are most likely to be supported by future iterations of the Flex SDK.

5.1. Handle a Button’s Click Event

Problem

You need to perform a task in response to user interaction, such as outputting a list of names to the console when the user clicks a button.

Solution

Use the click event attribute of the s:Button tag to assign a handler for the event in MXML. Alternatively, in ActionScript, use the addEventListener() method on the button instance to assign a listener for the click event.

Discussion

The following code shows how to listen for a button click by using MXML to assign a handler for the click event attribute of the s:Button tag:

<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required