Displaying a drop shadow behind a VBox container in Flex

The following example shows how you can display a drop shadow behind a Flex VBox container by setting the borderStyle and dropShadowEnabled styles.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/09/24/displaying-a-drop-shadow-behind-a-vbox-container-in-flex/ -->
<mx:Application name="VBox_dropShadowEnabled_test"
        xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:VBox id="vBox"
            dropShadowEnabled="true"
            borderStyle="solid"
            borderThickness="0"
            backgroundColor="red"
            width="100%"
            height="100%">
        <mx:Label text="{Capabilities.version}"
                fontSize="64" />
    </mx:VBox>

</mx:Application>

View source is enabled in the following example.

You can also set the borderStyle and dropShadowEnabled styles in an external .CSS file or <mx:Style /> block, as seen in the following example:

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/09/24/displaying-a-drop-shadow-behind-a-vbox-container-in-flex/ -->
<mx:Application name="VBox_dropShadowEnabled_test"
        xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Style>
        VBox {
            dropShadowEnabled: true;
            borderStyle: solid;
            borderThickness: 0;
            backgroundColor: red;
        }
    </mx:Style>

    <mx:VBox id="vBox"
            width="100%"
            height="100%">
        <mx:Label text="{Capabilities.version}"
                fontSize="64" />
    </mx:VBox>

</mx:Application>

Or, you can set the borderStyle and dropShadowEnabled styles using ActionScript, as seen in the following example:

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/09/24/displaying-a-drop-shadow-behind-a-vbox-container-in-flex/ -->
<mx:Application name="VBox_dropShadowEnabled_test"
        xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Script>
        <![CDATA[
            private function init():void {
                vBox.setStyle("dropShadowEnabled", true);
                vBox.setStyle("borderStyle", "solid");
                vBox.setStyle("borderThickness", 0);
                vBox.setStyle("backgroundColor", "red");
            }
        ]]>
    </mx:Script>

    <mx:VBox id="vBox"
            width="100%"
            height="100%"
            initialize="init();">
        <mx:Label text="{Capabilities.version}"
                fontSize="64" />
    </mx:VBox>

</mx:Application>

Due to popular demand, here is the “same” example in a more ActionScript friendly format:

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/09/24/displaying-a-drop-shadow-behind-a-vbox-container-in-flex/ -->
<mx:Application name="VBox_dropShadowEnabled_test"
        xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white"
        initialize="init();">

    <mx:Script>
        <![CDATA[
            import mx.controls.Label;
            import mx.containers.VBox;

            private var vBox:VBox;

            private function init():void {
                var lbl:Label = new Label();
                lbl.text = Capabilities.version;
                lbl.setStyle("fontSize", 64);

                vBox = new VBox();
                vBox.setStyle("dropShadowEnabled", true);
                vBox.setStyle("borderStyle", "solid");
                vBox.setStyle("borderThickness", 0);
                vBox.setStyle("backgroundColor", "red");
                vBox.percentWidth = 100;
                vBox.percentHeight = 100;
                vBox.addChild(lbl);
                addChild(vBox);
            }
        ]]>
    </mx:Script>

</mx:Application>

3 thoughts on “Displaying a drop shadow behind a VBox container in Flex

  1. Hi,
    Please reply to this post ….
    I have one array list will be loaded using HttpService. I have one Vbox component. In that I have 4 images. Based on the size of the array I need to show these images. I am not getting which event to catch to know the value of array as most of the I am getting array as null
    Regards,
    Krishna

Comments are closed.