Setting the corner radius on the Spark TitleWindow container in Flex 4

The following example shows how you can set the corner radius on the Spark TitleWindow container in Flex 4 by setting the cornerRadius 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/27/setting-the-corner-radius-on-the-spark-titlewindow-container-in-flex-4/ -->
<s:Application name="Spark_TitleWindow_cornerRadius_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:layout>
        <s:HorizontalLayout horizontalAlign="center" verticalAlign="middle" />
    </s:layout>
    <s:controlBarContent>
        <s:Label text="cornerRadius:" />
        <s:HSlider id="sl" maximum="15" />
    </s:controlBarContent>
 
    <s:TitleWindow id="tw1"
            title="Spark TitleWindow without control bar"
            cornerRadius="{sl.value}"
            controlBarVisible="true"
            width="250" height="120"
            horizontalCenter="0" verticalCenter="0">
        <s:Label text="Spark TitleWindow contents" left="10" top="10" />
    </s:TitleWindow>
 
    <s:TitleWindow id="tw2"
            title="Spark TitleWindow with control bar"
            cornerRadius="{sl.value}"
            controlBarVisible="true"
            width="250" height="120"
            horizontalCenter="0" verticalCenter="0">
        <s:controlBarContent>
            <s:Label text="Control bar content" />
        </s:controlBarContent>
        <s:Label text="Spark TitleWindow contents" left="10" top="10" />
    </s:TitleWindow>
 
</s:Application>

[GoogleAdsWide]

View source is enabled in the following example.

You can also set the cornerRadius 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/27/setting-the-corner-radius-on-the-spark-titlewindow-container-in-flex-4/ -->
<s:Application name="Spark_TitleWindow_cornerRadius_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:layout>
        <s:HorizontalLayout horizontalAlign="center" verticalAlign="middle" />
    </s:layout>
 
    <fx:Style>
        @namespace s "library://ns.adobe.com/flex/spark";
        @namespace mx "library://ns.adobe.com/flex/mx";
 
        s|TitleWindow {
            cornerRadius: 15;
        }
    </fx:Style>
 
    <s:TitleWindow id="tw1"
                   title="Spark TitleWindow without control bar"
                   controlBarVisible="true"
                   width="280" height="120"
                   horizontalCenter="0" verticalCenter="0">
        <s:Label text="Spark TitleWindow contents" left="10" top="10" />
    </s:TitleWindow>
 
    <s:TitleWindow id="tw2"
                   title="Spark TitleWindow with control bar"
                   controlBarVisible="true"
                   width="280" height="120"
                   horizontalCenter="0" verticalCenter="0">
        <s:controlBarContent>
            <s:Label text="Control bar content" />
        </s:controlBarContent>
        <s:Label text="Spark TitleWindow contents" left="10" top="10" />
    </s:TitleWindow>
 
</s:Application>

Or you can se the cornerRadius style using ActionScript, as seen in the following example:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2010/04/27/setting-the-corner-radius-on-the-spark-titlewindow-container-in-flex-4/ -->
<s:Application name="Spark_TitleWindow_cornerRadius_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:layout>
        <s:HorizontalLayout horizontalAlign="center" verticalAlign="middle" />
    </s:layout>
    <s:controlBarContent>
        <s:Label text="cornerRadius:" />
        <s:HSlider id="sl" maximum="15" change="sl_changeHandler(event);" />
    </s:controlBarContent>
 
    <fx:Script>
        <![CDATA[
            protected function sl_changeHandler(evt:Event):void {
                tw1.setStyle("cornerRadius", sl.value);
                tw2.setStyle("cornerRadius", sl.value);
            }
        ]]>
    </fx:Script>
 
    <s:TitleWindow id="tw1"
                   title="Spark TitleWindow without control bar"
                   controlBarVisible="true"
                   width="280" height="120"
                   horizontalCenter="0" verticalCenter="0">
        <s:Label text="Spark TitleWindow contents" left="10" top="10" />
    </s:TitleWindow>
 
    <s:TitleWindow id="tw2"
                   title="Spark TitleWindow with control bar"
                   controlBarVisible="true"
                   width="280" height="120"
                   horizontalCenter="0" verticalCenter="0">
        <s:controlBarContent>
            <s:Label text="Control bar content" />
        </s:controlBarContent>
        <s:Label text="Spark TitleWindow contents" left="10" top="10" />
    </s:TitleWindow>
 
</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.

5 thoughts on “Setting the corner radius on the Spark TitleWindow container in Flex 4

  1. Thanks for the example, as always. You switched the titles of the title windows, though… the one that says with control bar doesn’t have one and vice-versa.

  2. Also, if you want to round the bottom corner and NOT have a control bar, it looks like you can set the controlBarVisible property to false at runtime using ActionScript. For more information, see http://bugs.adobe.com/jira/browse/SDK-26317.

    <s:TitleWindow id="tw3"
            title="Spark TitleWindow without control bar"
            cornerRadius="15"
            width="280" height="120"
            horizontalCenter="0" verticalCenter="0"
            creationComplete="tw3.controlBarVisible = false;">
        <s:controlBarContent />
        <s:Label text="Spark TitleWindow contents" left="10" top="10" />
    </s:TitleWindow>

    Peter

  3. It looks like the same thing happens to the Spark Panel corner radius. The top rounds off, but the bottom does not. The workaround does not seem to work for panels though. Any suggestions?

    1. @Robert M,

      Sorry for the slow reply, but you can solve it by creating a custom Spark Panel skin and modifying the logic slightly to not consider the presence (or absence) of a control bar.

      Basically copy the default Spark Panel skin and change lines 58-60 to say this instead:

      var withControls:Boolean = true;

      Or you could just remove all references to the withControls which would cause the code to always round Panel corners.

      Peter

Comments are closed.