17
Sep
08

Setting a custom divider color on the DividedBox container in Flex

The following example shows how you can set a custom divider color on a Flex DividedBox container (HDividedBox or VDividedBox) by setting the dividerColor style.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/09/17/setting-a-custom-divider-color-on-the-dividedbox-container-in-flex/ -->
<mx:Application name="HDividedBox_dividerColor_test"
        xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:ApplicationControlBar dock="true">
        <mx:Form styleName="plain">
            <mx:FormItem label="dividerColor:">
                <mx:ColorPicker id="colorPicker" />
            </mx:FormItem>
        </mx:Form>
    </mx:ApplicationControlBar>

    <mx:HDividedBox id="dividedBox"
            dividerColor="{colorPicker.selectedColor}"
            width="100%"
            height="100%">
        <mx:Box id="box1"
                backgroundColor="haloGreen"
                width="100%"
                height="100%"
                minWidth="100" />
        <mx:Box id="box2"
                backgroundColor="haloBlue"
                width="100%"
                height="100%"
                minWidth="100" />
    </mx:HDividedBox>

</mx:Application>

View source is enabled in the following example.

You can also set the dividerColor style 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/17/setting-a-custom-divider-color-on-the-dividedbox-container-in-flex/ -->
<mx:Application name="HDividedBox_dividerColor_test"
        xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Style>
        HDividedBox {
            dividerColor: red;
        }
    </mx:Style>

    <mx:HDividedBox id="dividedBox"
            width="100%"
            height="100%">
        <mx:Box id="box1"
                backgroundColor="haloGreen"
                width="100%"
                height="100%"
                minWidth="100" />
        <mx:Box id="box2"
                backgroundColor="haloBlue"
                width="100%"
                height="100%"
                minWidth="100" />
    </mx:HDividedBox>

</mx:Application>

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

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/09/17/setting-a-custom-divider-color-on-the-dividedbox-container-in-flex/ -->
<mx:Application name="HDividedBox_dividerColor_test"
        xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Script>
        <![CDATA[
            import mx.events.ColorPickerEvent;

            private function colorPicker_change(evt:ColorPickerEvent):void {
                dividedBox.setStyle("dividerColor", evt.color);
            }
        ]]>
    </mx:Script>

    <mx:ApplicationControlBar dock="true">
        <mx:Form styleName="plain">
            <mx:FormItem label="dividerColor:">
                <mx:ColorPicker id="colorPicker"
                        change="colorPicker_change(event);" />
            </mx:FormItem>
        </mx:Form>
    </mx:ApplicationControlBar>

    <mx:HDividedBox id="dividedBox"
            width="100%"
            height="100%">
        <mx:Box id="box1"
                backgroundColor="haloGreen"
                width="100%"
                height="100%"
                minWidth="100" />
        <mx:Box id="box2"
                backgroundColor="haloBlue"
                width="100%"
                height="100%"
                minWidth="100" />
    </mx:HDividedBox>

</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/17/setting-a-custom-divider-color-on-the-dividedbox-container-in-flex/ -->
<mx:Application name="HDividedBox_dividerColor_test"
        xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white"
        initialize="init();">

    <mx:Script>
        <![CDATA[
            import mx.containers.ApplicationControlBar;
            import mx.containers.Box;
            import mx.containers.Form;
            import mx.containers.FormItem;
            import mx.containers.HDividedBox;
            import mx.controls.ColorPicker;
            import mx.events.ColorPickerEvent;

            private var colorPicker:ColorPicker;
            private var box1:Box;
            private var box2:Box;
            private var dividedBox:HDividedBox;

            private function init():void {
                colorPicker = new ColorPicker();
                colorPicker.addEventListener(ColorPickerEvent.CHANGE,
                            colorPicker_change);

                var formItem:FormItem = new FormItem();
                formItem.label = "dividerColor:";
                formItem.addChild(colorPicker);

                var form:Form = new Form();
                form.styleName = "plain";
                form.addChild(formItem);

                var appControlBar:ApplicationControlBar;
                appControlBar = new ApplicationControlBar();
                appControlBar.dock = true;
                appControlBar.addChild(form);
                addChildAt(appControlBar, 0);

                box1 = new Box();
                box1.percentWidth = 100;
                box1.percentHeight = 100;
                box1.minWidth = 100;
                box1.setStyle("backgroundColor", "haloGreen");

                box2 = new Box();
                box2.percentWidth = 100;
                box2.percentHeight = 100;
                box2.minWidth = 100;
                box2.setStyle("backgroundColor", "haloBlue");

                dividedBox = new HDividedBox();
                dividedBox.percentWidth = 100;
                dividedBox.percentHeight = 100;
                dividedBox.addChild(box1);
                dividedBox.addChild(box2);
                addChild(dividedBox);
            }

            private function colorPicker_change(evt:ColorPickerEvent):void {
                dividedBox.setStyle("dividerColor", evt.color);
            }
        ]]>
    </mx:Script>

</mx:Application>

1 Response to “Setting a custom divider color on the DividedBox container in Flex”


  1. 1 codecraig Sep 18th, 2008 at 6:58 pm

    Is there a nice way to auto-hide the divided box? or allow the user to click on the divider box button and have it collapse?

Leave a Reply

This blog is terrible at eating HTML tags. If you plan on posting code/XML, please escape your "<" characters as "&lt;" and your ">" characters as "&gt;".




September 2008
M T W T F S S
« Aug   Oct »
1234567
891011121314
15161718192021
22232425262728
2930  

Badge Farm

  • Powered by Redoable 1.2
  • Cornify
  • Feeds burnt by Feedburner
  • Feed