Archive for category Flex

Sort Arraycollection based on date field in Flex

If you have ArrayCollection as Dataprovider to Datagrid and you want to sort dataprovider based on single date field for ex: created_Date or modified_Date, then below is code snippet:

public function sortOnFilterModifiedDate():void
var dataSortField:SortField = new SortField(); = “modified_Date”;//date field
dataSortField.numeric = false;
dataSortField.descending = true;//true or false do not affect sorting logic
dataSortField.caseInsensitive = true;

var filterModifiedDateSort:Sort = new Sort();
filterModifiedDateSort.fields = [dataSortField];
filterModifiedDateSort.compareFunction = filterModifiedDateSortCompareFunc;

pageFilters.sort = filterModifiedDateSort;


private function filterModifiedDateSortCompareFunc (itemA:Object,itemB:Object,fields:Array):int
return ObjectUtil.dateCompare(DateFormatter.parseDateString(itemA.modified_Date),


Use below code to sort in Descending order: -1*ObjectUtil.dateCompare(DateFormatter.parseDateString(itemA.modified_Date),




Leave a comment

Gradient background container with button as icon with states


<s:Group width=”170″ height=”20″>
<s:Rect left=”0″ top=”0″ right=”0″ bottom=”0″>
<s:SolidColorStroke color=”#e9e9e9″ />
<s:LinearGradient rotation=”-90″>
<s:GradientEntry color=”#cccccc” ratio=”.4″ />
<s:GradientEntry color=”#eeeeee” />
<s:Label left=”5″ right=”5″ top=”2″ bottom=”2″
text=”title” fontSize=”10″
verticalAlign=”middle” />
<s:Button width=”10″ height=”10″ right=”8″ top=”3″
buttonMode=”true” click=”clickHandler(event)”



<?xml version=”1.0″ encoding=”utf-8″?>
<s:Skin xmlns:fx=”;
<!– host component –>

<!– states –>
<s:State name=”disabled” />
<s:State name=”down” stateGroups=”stateUp” />
<s:State name=”over” />
<s:State name=”up” stateGroups=”stateUp” />

<s:Group visible.stateUp=”false” visible=”true” buttonMode=”true”>
<s:BitmapImage source=”@Embed(‘../images/icon_maximize_up.png’)” />
<s:Group visible.stateUp=”true” visible=”false” buttonMode=”true”>
<s:BitmapImage source=”@Embed(‘../images/icon_maximize_normal.png’)” />


, ,

Leave a comment

Flex rich text editor with toolbar at top instead of bottom

<mx:RichTextEditor id=”richTextEditor” creationComplete=”richTextEditor_creationCompleteHandler(event)”
width=”550″ height=”200″
headerHeight=”0″ />


protected function richTextEditor_creationCompleteHandler(event:FlexEvent):void


/* toolbar is in ControlBar, ControlBar is not a direct content
child of the Panel in RichTextEditor. */

controlBarContainer = new HBox();
controlBarContainer.percentWidth = 100;
controlBarContainer.setStyle(‘paddingLeft’, 5);
controlBarContainer.setStyle(‘paddingTop’, 5);
controlBarContainer.setStyle(‘paddingRight’, 5);
controlBarContainer.setStyle(‘paddingBottom’, 5);
richTextEditor.addChildAt(controlBarContainer, 0);


Leave a comment

Singleton class for Localization in Flex

Default way of using resourceManager


We have customized way of using resourceManager by using singleton class : ResourceUtil

Modified syntax of using ResourceUtil


— It reduced the code by not using bundleName each time. Find the code below to create singleton class

public class ResourceUtil
public static const DEFAULT_BUNDLE:String = ‘ResourceFileName’;

public static function string(key:String,parameters:Array=null):String
return ResourceLoader.instance().getString(DEFAULT_BUNDLE,key,parameters);
} :


import mx.resources.IResourceManager;
import mx.resources.ResourceManager;

public class ResourceLoader extends EventDispatcher
public static const DEFAULT_LOCALE:String=’en_US’ ;
public static var _instance:ResourceLoader ;
private var _resourceManager:IResourceManager ;

public static function instance():ResourceLoader
if(_instance == null)
_instance = new ResourceLoader(PrivateSingletonEnforcer);

return _instance ;

public function ResourceLoader(singletonEnforcer:Class)
throw new Error(‘ResourceLoader cannot be instantiated directly.’)
_resourceManager = ResourceManager.getInstance();

public function getString(bundleName:String,resourceName:String,parameters:Array = null):String
var value:String = _resourceManager.getString(bundleName,resourceName,parameters);
if(value == null)
return resourceName ;
return value ;


class PrivateSingletonEnforcer

1 Comment

Center tag: keep everything in middle




… put any content, it will be aligned in the center of browser page
… I think we have to keep content size lesser than browser size
to fit into middle






Leave a comment

Unique Random Generator

The following code generates the unique random numbers.
User will input max length (for ex: 10 unique random numbers from 0 to 10) in textinput , and on keyup event values are populated in text area one below another.

<?xml version=”1.0″ encoding=”utf-8″?>
<mx:Application xmlns:mx=”; layout=”vertical” width=”100%” height=”100%”
backgroundAlpha=”1″ backgroundGradientAlphas=”[1,1]” backgroundGradientColors=”[#999999,#999999]”>
import mx.utils.ObjectUtil;
private function calUniqueRandomNo(maxCount:Number):void
resText.text = “”;
var resArr:Array = new Array();

var randNo:int = parseInt((Math.random()*maxCount).toString()) ;

resText.text += resArr.toString();

private function search(valueToSearch:int,arrayToSearch:Array):Boolean
for(var j:int=0;j<arrayToSearch.length;j++)
if(arrayToSearch[j]== valueToSearch)
return true;
return false;
<mx:FormItem label=”Count:”>

<mx:TextInput id=”maxCountIp” keyUp=”{calUniqueRandomNo(Number(maxCountIp.text))}” />

<mx:TextArea id=”resText” width=”70″ height=”70%”/>

If any other way , welcome to post as comment.


Leave a comment

Flex Builder Key Assist

Use following key shortcuts, as they help in faster code development
1) select the code line or lines, then Ctrl + Alt + Up Arrow Key or Ctrl + Alt + Down Arrow Key
— copy the selected line or lines of code on upside/downside of the selected line or lines

2) Ctrl + O
— opens the window with all variables , functions and controls in current .mxml or .as file

3) Ctrl + Shift +R
— opens the window with list of all files of all open projects in the workspace of Flex builder
— very useful if you remember the filename of the file which¬† you want to open.

4) Ctrl + Shift +F
— opens the search window and search throught files of all open projects in the workspace of Flex builder

Enjoy great coding of Flex !!I will post some more in future. You can also find key assist in Flex builder by Ctrl+Shift+L.

Leave a comment