Setting the content background alpha on a Spark Panel container in Flex 4

The following example shows how you can set the content background alpha on a Spark Panel container’s children in Flex 4 by setting the contentBackgroundAlpha style.

This example requires Flex SDK 4.0.0.10007 or later.

The following example(s) require Flash Player 10 and the Adobe Flex 4 SDK. To download the Adobe Flash Builder 4 trial, see www.adobe.com/products/flex/. To download the latest nightly build of the Flex 4 SDK, see 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/06/setting-the-content-background-alpha-on-a-spark-panel-container-in-flex-4/ -->
<s:Application name="Spark_Panel_contentBackgroundAlpha_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/mx">
    <s:controlBarContent>
        <mx:Form>
            <mx:FormItem label="contentBackgroundAlpha:">
                <s:HSlider id="slider"
                        minimum="0.0"
                        maximum="1.0"
                        value="1.0"
                        snapInterval="0.1"
                        stepSize="0.1" />
            </mx:FormItem>
            <mx:FormItem label="contentBackgroundColor:">
                <mx:ColorPicker id="colorPicker" selectedColor="white" />
            </mx:FormItem>
        </mx:Form>
    </s:controlBarContent>
 
    <s:Panel id="pnl"
            title="Spark Panel title"
            backgroundColor="red"
            contentBackgroundAlpha="{slider.value}"
            contentBackgroundColor="{colorPicker.selectedColor}"
            horizontalCenter="0" verticalCenter="0">
        <s:TextArea id="sTA" left="10" right="10" top="10" bottom="10">
            <s:text><fx:String source="lorem.txt" /></s:text>
        </s:TextArea>
    </s:Panel>
 
</s:Application>

[GoogleAdsWide]

View source is enabled in the following example.

You can also set the contentBackgroundAlpha style in an external .CSS file or an <Style/> block, as seen in the following example:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/09/06/setting-the-content-background-alpha-on-a-spark-panel-container-in-flex-4/ -->
<s:Application name="Spark_Panel_contentBackgroundAlpha_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/mx">
 
    <fx:Style>
        @namespace s "library://ns.adobe.com/flex/spark";
        @namespace mx "library://ns.adobe.com/flex/mx";
 
        s|Panel {
            backgroundColor: red;
            contentBackgroundAlpha: 0.3;
            contentBackgroundColor: blue;
        }
    </fx:Style>
 
    <s:Panel id="pnl"
            title="Spark Panel title"
            horizontalCenter="0" verticalCenter="0">
        <s:TextArea id="sTA" left="10" right="10" top="10" bottom="10">
            <s:text><fx:String source="lorem.txt" /></s:text>
        </s:TextArea>
    </s:Panel>
 
</s:Application>

Or you can set the contentBackgroundAlpha style using ActionScript, as seen in the following example:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/09/06/setting-the-content-background-alpha-on-a-spark-panel-container-in-flex-4/ -->
<s:Application name="Spark_Panel_contentBackgroundAlpha_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/mx">
    <s:controlBarContent>
        <mx:Form>
            <mx:FormItem label="contentBackgroundAlpha:">
                <s:HSlider id="slider"
                        minimum="0.0"
                        maximum="1.0"
                        value="1.0"
                        snapInterval="0.1"
                        stepSize="0.1"
                        change="slider_changeHandler(event);"/>
            </mx:FormItem>
            <mx:FormItem label="contentBackgroundColor:">
                <mx:ColorPicker id="colorPicker"
                        selectedColor="white"
                        change="colorPicker_changeHandler(event);"/>
            </mx:FormItem>
        </mx:Form>
    </s:controlBarContent>
 
    <fx:Script>
        <![CDATA[
            import mx.events.ColorPickerEvent;
 
            protected function slider_changeHandler(evt:Event):void {
                pnl.setStyle("contentBackgroundAlpha", evt.currentTarget.value);
            }
 
            protected function colorPicker_changeHandler(evt:ColorPickerEvent):void {
                pnl.setStyle("contentBackgroundColor", evt.color);
            }
 
            protected function init():void {
                pnl.setStyle("backgroundColor", "red");
            }
        ]]>
    </fx:Script>
 
    <s:Panel id="pnl"
             title="Spark Panel title"
             horizontalCenter="0" verticalCenter="0"
             initialize="init();">
        <s:TextArea id="sTA" left="10" right="10" top="10" bottom="10">
            <s:text><fx:String source="lorem.txt" /></s:text>
        </s:TextArea>
    </s:Panel>
 
</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.

One thought on “Setting the content background alpha on a Spark Panel container in Flex 4

Comments are closed.