Setting the chrome color on a Spark Panel container in Flex 4

The following example shows how you can set the chrome color on a Spark Panel container in Flex 4 by setting the chromeColor style.

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/2010/04/29/setting-the-chrome-color-on-a-spark-panel-container-in-flex-4/ -->
<s:Application name="Spark_Panel_chromeColor_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="chromeColor:">
                <mx:ColorPicker id="clrPckr" selectedColor="red" />
            </mx:FormItem>
            <mx:FormItem label="controlBarVisible">
                <s:CheckBox id="chckBx" selected="true" />
            </mx:FormItem>
        </mx:Form>
    </s:controlBarContent>
 
    <s:Panel id="pnl"
            title="Spark Panel"
            chromeColor="{clrPckr.selectedColor}"
            controlBarVisible="{chckBx.selected}"
            left="40" right="40" top="40" bottom="40">
        <s:controlBarContent>
            <s:Label text="Panel control bar contents" />
        </s:controlBarContent>
 
        <s:Button label="Panel contents" x="20" y="20" />
    </s:Panel>
 
</s:Application>

[GoogleAdsWide]

View source is enabled in the following example.

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

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2010/04/29/setting-the-chrome-color-on-a-spark-panel-container-in-flex-4/ -->
<s:Application name="Spark_Panel_chromeColor_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 {
            chromeColor: haloBlue;
        }
    </fx:Style>
 
    <s:Panel id="pnl"
            title="Spark Panel"
            left="40" right="40" top="40" bottom="40">
        <s:controlBarContent>
            <s:Label text="Panel control bar contents" />
        </s:controlBarContent>
 
        <s:Button label="Panel contents" x="20" y="20" />
    </s:Panel>
 
</s:Application>

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

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2010/04/29/setting-the-chrome-color-on-a-spark-panel-container-in-flex-4/ -->
<s:Application name="Spark_Panel_chromeColor_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="chromeColor:">
                <mx:ColorPicker id="clrPckr"
                        selectedColor="#CCCCCC"
                        change="clrPckr_changeHandler(event);" />
            </mx:FormItem>
            <mx:FormItem label="controlBarVisible">
                <s:CheckBox id="chckBx"
                        selected="true"
                        change="chckBx_changeHandler(event);" />
            </mx:FormItem>
        </mx:Form>
    </s:controlBarContent>
 
    <fx:Script>
        <![CDATA[
            import mx.events.ColorPickerEvent;
 
            protected function clrPckr_changeHandler(evt:ColorPickerEvent):void {
                pnl.setStyle("chromeColor", evt.color);
            }
 
            protected function chckBx_changeHandler(evt:Event):void {
                pnl.controlBarVisible = chckBx.selected;
            }
        ]]>
    </fx:Script>
 
    <s:Panel id="pnl"
            title="Spark Panel"
            left="40" right="40" top="40" bottom="40">
        <s:controlBarContent>
            <s:Label text="Panel control bar contents" />
        </s:controlBarContent>
 
        <s:Button label="Panel contents" x="20" y="20" />
    </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.

9 thoughts on “Setting the chrome color on a Spark Panel container in Flex 4

  1. I’m landing up with this error when I try this code in FB4

    Description Resource Path Location Type
    An internal build error has occurred. Right-click for more information. F4POC Unknown Flex Problem

    Can anyone point me, where I’m going wrong as the projects are working fine

    1. Sorry if i was unclear before but I would like to target the headers of all panels and change the text color only. Is there a css function/selector for this?
      instead I have add a long list of components and specifically change their colors to black so that they don’t inherit the panels’ white color.
      Thanks in advance

      s|Panel, s|Scroller {
      	chromeColor:#6086b0;
      	/* color:white; */
      }
       
      mx|Tree, s|DataGroup, s|ItemRenderer, mx|Form {
      	color:black;
      	fontSize:13px;
      }
  2. One more Question. Seems like styling a button also styles a dropdownlist at least in my project. Have you seen this before?
    My stylesheet is pretty slim so i don’t see any conflicting styles.

    s|DropDownList{
    	chromeColor:#D1CFCF;
    }
     
    s|Button{
    	chromeColor:#272727;
    	color:white;
    }
    1. @Alex,

      I haven’t seen that exact problem before, but I’m not overly surprised (you’ll see fun issues like this if you try and globally style s|Label as it will cascade to s:Button and everywhere else).

      The main issue is that the Spark DropDownList control includes an s:Button in its skin, and that is cascading down. To work around the issue you can explicitly style the s|DropDownList control’s Button using some advanced CSS, as seen in the following example:

      <?xml version="1.0" encoding="utf-8"?>
      <s:Application 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|Button{
                  chromeColor: purple;
                  color:white;
              }
       
              s|DropDownList s|Button#openButton {
                  chromeColor: red;
              }
          </fx:Style>
       
          <s:layout>
              <s:HorizontalLayout horizontalAlign="center" paddingTop="20" />
          </s:layout>
       
          <s:Button label="Spark Button" />
          <s:DropDownList prompt="Spark DropDownList" />
       
      </s:Application>

      Peter

Comments are closed.