The following example shows how you can toggle the application control bar in a Spark Application container in Flex 4 by setting the Boolean controlBarVisible property.

The controlBarVisible property was added in Flex 4.0.0.10693 beta SDK.

The following example(s) require Flash Player 10 and the Adobe Flex 4 SDK. To download the Adobe Flash Builder 4 trial, see http://www.adobe.com/products/flex/. To download the latest nightly build of the Flex 4 SDK, see http://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/10/03/toggling-the-application-control-bar-in-a-spark-application-container-in-flex-4/ -->
<s:Application name="Spark_Application_controlBarVisible_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"
        controlBarVisible="{chckBx.selected}">
    <s:controlBarContent>
        <s:Label id="sdkVer" initialize="sdkVer.text = mx_internal::VERSION;" />
    </s:controlBarContent>
 
    <s:CheckBox id="chckBx"
            label="controlBarVisible"
            selected="true"
            horizontalCenter="0" verticalCenter="0" />
 
</s:Application>

View source is enabled in the following example.

You can also set the controlBarVisible property using ActionScript, as seen in the following example:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/10/03/toggling-the-application-control-bar-in-a-spark-application-container-in-flex-4/ -->
<s:Application name="Spark_Application_controlBarVisible_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>
        <s:Label id="sdkVer" initialize="sdkVer.text = mx_internal::VERSION;" />
    </s:controlBarContent>
 
    <fx:Script>
        <![CDATA[
            import mx.core.FlexGlobals;
 
            protected function chckBx_changeHandler(evt:Event):void {
                Application(FlexGlobals.topLevelApplication).controlBarVisible = chckBx.selected;
            }
        ]]>
    </fx:Script>
 
    <s:CheckBox id="chckBx"
            label="controlBarVisible"
            selected="true"
            horizontalCenter="0" verticalCenter="0"
            change="chckBx_changeHandler(event);"/>
 
</s:Application>

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

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/10/03/toggling-the-application-control-bar-in-a-spark-application-container-in-flex-4/ -->
<s:Application name="Spark_Application_controlBarVisible_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"
        initialize="init();">
 
    <fx:Script>
        <![CDATA[
            import mx.core.FlexGlobals;
 
            import spark.components.CheckBox;
            import spark.components.Label;
 
            private var chckBx:CheckBox;
 
            private function init():void {
                var sdkVer:Label = new Label();
                sdkVer.text = mx_internal::VERSION;
                Application(FlexGlobals.topLevelApplication).controlBarContent = [sdkVer];
 
                chckBx = new CheckBox();
                chckBx.label = "controlBarVisible";
                chckBx.selected = true;
                chckBx.horizontalCenter = 0;
                chckBx.verticalCenter = 0;
                chckBx.addEventListener(Event.CHANGE, chckBx_changeHandler);
                addElement(chckBx);
            }
 
            protected function chckBx_changeHandler(evt:Event):void {
                Application(FlexGlobals.topLevelApplication).controlBarVisible = chckBx.selected;
            }
        ]]>
    </fx:Script>
 
</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.

 
Tagged with:
 
About The Author

Peter deHaan

Peter deHaan currently works for Adobe on the Flex SDK QA team. While not working on Flex, Flash, and ColdFusion applications, Peter enjoys making up bios and writing in 3rd person. Peter's rarely updated blog can be found at blogs.adobe.com/pdehaan/, actionscriptexamples.com, airexamples.com, and coldfusionexamples.com.

10 Responses to Toggling the application control bar in a Spark Application container in Flex 4

  1. david r says:

    bring back the compiled examples! I don’t have the nightly SDK, just the beta 2…I’d like to see what it looks like without having to install a new SDK

    keep up the good work!

    • Peter deHaan says:

      @david r,

      Yeah, I’ve been thinking now that the Flex 4 SDK is a lot less stable w/ fewer renames, I should start trying to post SWF examples again, but unfortunately they take at least 4x longer to do since I need to create a new project and disable RSLs/history management, create readme files, export release builds, upload via FTP, etc.

      Peter

  2. Tom says:

    Why they have changed alsmot everything ? ;-( ;-(

    How is it possible to set the Application Control Bar width, height and fill colors now ?

    cheers

  3. joe says:

    Great Blog Peter

    But what are Adobe thinking ????

    In flex 3 I could code an effective application control bar in a couple of lines.

    In flex 4 I seem to be looking at a hundred or more lines of code to accomplish the same thing? And this is an improvement ?

    This is crazy !

    • Peter deHaan says:

      To set the application control bar, set the controlBarContent property. To toggle the control bar’s visibility, set the Boolean controlBarVisible property. Neither of those require hundreds of lines of code.

      Peter

  4. Devtron says:

    why in the world would this use FLEXGlobals to achieve this?
    why in the world would this not even explain FLEXGlobals? I mean, afterall, the example uses it. This could confuse beginners.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Anti-Spam Protection by WP-SpamFree