The following example shows how you can set the focus alpha on the Spark TextArea control in Flex 4 by setting the focusAlpha style.

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 trial, see http://www.adobe.com/products/flex/. To download the latest nightly build of the Flex 4 SDK, see http://opensource.adobe.com/wiki/display/flexsdk/Download+Flex+4.
For more information on getting started with Flex 4 and Flash Builder 4, see the official Adobe Flex Team blog.

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/09/08/setting-the-focus-alpha-on-a-spark-textarea-control-in-flex-4/ -->
<s:Application name="Spark_TextArea_focusAlpha_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/halo">
    <s:controlBarContent>
        <mx:Form contentBackgroundAlpha="0">
            <mx:FormItem label="focusAlpha:">
                <s:HSlider id="slider"
                        minimum="0.0"
                        maximum="1.0"
                        value="0.8"
                        snapInterval="0.1"
                        stepSize="0.1"
                        change="txtArea.setFocus();" />
            </mx:FormItem>
            <mx:FormItem label="focusColor:">
                <mx:ColorPicker id="colorPicker"
                        selectedColor="haloBlue"
                        change="callLater(txtArea.setFocus);" />
            </mx:FormItem>
        </mx:Form>
    </s:controlBarContent>
 
    <s:TextArea id="txtArea"
            focusAlpha="{slider.value}"
            focusColor="{colorPicker.selectedColor}"
            horizontalCenter="0" verticalCenter="0"
            creationComplete="txtArea.setFocus();"/>
 
</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.

 
Tagged with:
 
About The Author

Peter deHaan

Peter deHaan currently works for Adobe on the Flex SDK QA team. While not working on Flex, Flash, and ColdFusion applications, Peter enjoys making up bios and writing in 3rd person. Peter's rarely updated blog can be found at blogs.adobe.com/pdehaan/, actionscriptexamples.com, airexamples.com, and coldfusionexamples.com.

2 Responses to Setting the focus alpha on a Spark TextArea control in Flex 4

  1. Tim S says:

    Mr Flex Ex’s,

    I’m curious, why was the callLater() necessary? Give us your insights.

    A Flex Friend

    • Peter deHaan says:

      @Tim S,

      It’s a timing thing. Initially I tried just calling txtArea.setFocus() from the ColorPicker control’s change handler, like so:

      <mx:ColorPicker id="colorPicker"
              selectedColor="haloBlue"
              change="txtArea.setFocus();" />

      But whenever I ran the code, the focus went back to the ColorPicker control. The ColorPicker control is a bit of a odd component. When you open the ColorPicker, the TextInput control in the pop up receives focus. When you close the ColorPicker control, the ColorPicker itself gets the focus. So long story longer, I had to use the callLater() function to delay the call to txtArea.setFocus() slightly so that the focus would correctly get set on the TextArea control.

      I agree, perhaps not the best solution (you will see a slight flicker as the focus goes from the ColorPicker back to the TextArea), but then again, I don’t suspect many people would be changing the focusAlpha and focusColor styles at runtime like this.

      Peter

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Anti-Spam Protection by WP-SpamFree