Setting the repeat delay on an auto repeating Spark Button control in Flex 4

The following example shows how you can set the repeat delay on an Flex 4 Spark Button control by setting the repeatDelay style and the autoRepeat property.

Full code after the jump.

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/2009/02/07/setting-the-repeat-delay-on-an-auto-repeating-fxbutton-control-in-flex-gumbo/ -->
<s:Application name="Spark_Button_repeatDelay_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/halo">
 
    <fx:Script>
        <![CDATA[
            import mx.events.FlexEvent;
 
            private function btn_buttonDown(evt:FlexEvent):void {
                var obj:Object = {};
                obj.type = evt.type;
                obj.time = new Date().toTimeString();
                arrColl.addItemAt(obj, 0);
            }
 
            private function btn_click(evt:MouseEvent):void {
                var obj:Object = {};
                obj.type = evt.type;
                obj.time = new Date().toTimeString();
                arrColl.addItemAt(obj, 0);
            }
        ]]>
    </fx:Script>
 
    <fx:Declarations>
        <s:ArrayCollection id="arrColl" />
    </fx:Declarations>
 
    <mx:ApplicationControlBar width="100%" cornerRadius="0">
        <s:Button id="btn"
                label="Spark Button (repeatDelay={btn.getStyle('repeatDelay')})"
                autoRepeat="true"
                repeatDelay="1000"
                buttonDown="btn_buttonDown(event);"
                click="btn_click(event);" />
    </mx:ApplicationControlBar>
 
    <mx:DataGrid id="dataGrid"
            dataProvider="{arrColl}"
            width="300"
            rowCount="6"
            horizontalCenter="0" verticalCenter="0">
        <mx:columns>
            <mx:DataGridColumn dataField="type" />
            <mx:DataGridColumn dataField="time" />
        </mx:columns>
    </mx:DataGrid>
 
</s:Application>

View source is enabled in the following example.

[GoogleAdsWide]

You can also set the repeatDelay 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/2009/02/07/setting-the-repeat-delay-on-an-auto-repeating-fxbutton-control-in-flex-gumbo/ -->
<s:Application name="Spark_Button_repeatDelay_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/halo">
 
    <fx:Style>
        @namespace s "library://ns.adobe.com/flex/spark";
        @namespace mx "library://ns.adobe.com/flex/halo";
 
        s|Button {
            repeatDelay: 1000; /* ms */
        }
    </fx:Style>
 
    <fx:Script>
        <![CDATA[
            import mx.events.FlexEvent;
 
            private function btn_buttonDown(evt:FlexEvent):void {
                var obj:Object = {};
                obj.type = evt.type;
                obj.time = new Date().toTimeString();
                arrColl.addItemAt(obj, 0);
            }
 
            private function btn_click(evt:MouseEvent):void {
                var obj:Object = {};
                obj.type = evt.type;
                obj.time = new Date().toTimeString();
                arrColl.addItemAt(obj, 0);
            }
        ]]>
    </fx:Script>
 
    <fx:Declarations>
        <s:ArrayCollection id="arrColl" />
    </fx:Declarations>
 
    <mx:ApplicationControlBar width="100%" cornerRadius="0">
        <s:Button id="btn"
                label="Spark Button (repeatDelay={btn.getStyle('repeatDelay')})"
                autoRepeat="true"
                buttonDown="btn_buttonDown(event);"
                click="btn_click(event);" />
    </mx:ApplicationControlBar>
 
    <mx:DataGrid id="dataGrid"
            dataProvider="{arrColl}"
            width="300"
            rowCount="6"
            horizontalCenter="0" verticalCenter="0">
        <mx:columns>
            <mx:DataGridColumn dataField="type" />
            <mx:DataGridColumn dataField="time" />
        </mx:columns>
    </mx:DataGrid>
 
</s:Application>

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

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/02/07/setting-the-repeat-delay-on-an-auto-repeating-fxbutton-control-in-flex-gumbo/ -->
<s:Application name="Spark_Button_repeatDelay_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/halo">
 
    <fx:Script>
        <![CDATA[
            import mx.events.FlexEvent;
 
            private function init():void {
                btn.setStyle("repeatDelay", 1000); /* ms */
            }
 
            private function btn_buttonDown(evt:FlexEvent):void {
                var obj:Object = {};
                obj.type = evt.type;
                obj.time = new Date().toTimeString();
                arrColl.addItemAt(obj, 0);
            }
 
            private function btn_click(evt:MouseEvent):void {
                var obj:Object = {};
                obj.type = evt.type;
                obj.time = new Date().toTimeString();
                arrColl.addItemAt(obj, 0);
            }
        ]]>
    </fx:Script>
 
    <fx:Declarations>
        <s:ArrayCollection id="arrColl" />
    </fx:Declarations>
 
    <mx:ApplicationControlBar width="100%" cornerRadius="0">
        <s:Button id="btn"
                label="Spark Button (repeatDelay={btn.getStyle('repeatDelay')})"
                autoRepeat="true"
                initialize="init();"
                buttonDown="btn_buttonDown(event);"
                click="btn_click(event);" />
    </mx:ApplicationControlBar>
 
    <mx:DataGrid id="dataGrid"
            dataProvider="{arrColl}"
            width="300"
            rowCount="6"
            horizontalCenter="0" verticalCenter="0">
        <mx:columns>
            <mx:DataGridColumn dataField="type" />
            <mx:DataGridColumn dataField="time" />
        </mx:columns>
    </mx:DataGrid>
 
</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.

One thought on “Setting the repeat delay on an auto repeating Spark Button control in Flex 4

Comments are closed.