The following example shows you how you can align the selected date in the Flex DateField control’s text field by setting the textAlign style.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/04/28/aligning-the-selected-date-in-a-datefield-control-in-flex/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="top"
        backgroundColor="white">

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

            private function init():void {
                dateField.selectedDate = new Date();
            }

            private function toggleButtonBar_itemClick(evt:ItemClickEvent):void {
                dateField.setStyle("textAlign", evt.item.label);
            }
        ]]>
    </mx:Script>

    <mx:ApplicationControlBar dock="true">
        <mx:ToggleButtonBar id="toggleButtonBar"
                selectedIndex="1"
                itemClick="toggleButtonBar_itemClick(event);">
            <mx:dataProvider>
                <mx:Array>
                    <mx:Object label="left" />
                    <mx:Object label="center" />
                    <mx:Object label="right" />
                </mx:Array>
            </mx:dataProvider>
        </mx:ToggleButtonBar>
    </mx:ApplicationControlBar>

    <mx:DateField id="dateField"
            width="200"
            textAlign="center"
            initialize="init();" />

</mx:Application>

View source is enabled in the following example.

 
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.

One Response to Aligning the selected date in a DateField control in Flex

  1. Klaus Busse says:

    Ehm, using the label for this is probably not the best coding style, especially when it comes to localisation… talking about localisation: Changing the language of a Flex application on the fly could be a nice tutorial as well!

    Thanks for all the stuff you provide here!

    Klaus

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