Creating a variable row height item renderer on a ComboBox control in Flex

The following example shows how you can create a variable row height item renderer on a Flex ComboBox control by setting the variableRowHeight property on the ComboBox control’s dropdown property.

Full code after the jump.


<?xml version="1.0" encoding="utf-8"?>
<!-- -->
<mx:Application xmlns:mx=""
            private function comboBox_open(evt:DropdownEvent):void {
                evt.currentTarget.dropdown.variableRowHeight = true;
    <mx:Array id="arr">
        <mx:Object name="Baltimore Orioles" abbr="BAL" />
        <mx:Object name="Boston Red Sox" abbr="BOS" />
        <mx:Object name="Chicago White Sox" abbr="CWS" />
        <mx:Object name="Cleveland Indians" abbr="CLE" />
        <mx:Object name="Detroit Tigers" abbr="DET" />
        <mx:Object name="Kansas City Royals" abbr="KC" />
        <mx:Object name="Los Angeles Angels of Anaheim" abbr="LAA" />
        <mx:Object name="Minnesota Twins" abbr="MIN" />
        <mx:Object name="New York Yankees" abbr="NYY" />
        <mx:Object name="Oakland Athletics" abbr="OAK" />
        <mx:Object name="Seattle Mariners" abbr="SEA" />
        <mx:Object name="Tampa Bay Devil Rays" abbr="TB" />
        <mx:Object name="Texas Rangers" abbr="TEX" />
        <mx:Object name="Toronto Blue Jays" abbr="TOR" />
    <mx:ComboBox id="comboBox"
                <mx:Text selectable="false"
                        toolTip="{} ({data.abbr})" />

View source is enabled in the following example.

10 thoughts on “Creating a variable row height item renderer on a ComboBox control in Flex

  1. Thanks for many examples.

    i hava a item renderer problem on DataGridColumn.
    There are comboboxes on DataGridColumn and users can choose a column item.
    But when grid is scrolled all DataGridColumns are initialized.
    In my opinion, Flex draw all grid again when scrolled.

    plz tell me the solution.

  2. If you’re trying to do this same thing without using an inline itemRenderer (e.g., just using the default ListItemRenderer, or a a custom renderer that extends that class), you also need to set the wordWrap property of the dropdown box to true:

    private function comboBox_open(evt:DropdownEvent):void 
         evt.currentTarget.dropdown.wordWrap = true;
         evt.currentTarget.dropdown.variableRowHeight = true;
  3. This was very helpful. Thanks.
    Is there anyway to adjust the height of the dropdown when it opens, other than the rowCount? I have two answers in one of my dropdown; the first is short and the second is long. The comboBox then sets the rowCount to 2 and determines the height of the dropdown by multiplying the rowCount by the height of the first row. This forces the user to scroll to see the entire second item.

    1. @Steve,

      Try setting the dropdown height in the open handler:

              private function comboBox_open(evt:DropdownEvent):void {
                  ComboBox(evt.currentTarget).dropdown.variableRowHeight = true;
                  ComboBox(evt.currentTarget).dropdown.height = 50;

      Because the open event is dispatched after the ComboBox is opened, you may want to set the ComboBox control’s openDuration style to 1ms so the resize isn’t as noticeable.


    2. Scott,
      This works for me.

      private var internalDropdownFactory:ClassFactory =
      new ClassFactory(SeparatorList);

      public class SeparatorList extends List {

      public function SeparatorList() {
      itemRenderer = new ClassFactory(SeparatorListItemRenderer);
      showDataTips = true;

      * @private
      * Determines if the itemrenderer is a separator. If so, return null to prevent separators
      * from highlighting and emitting menu-level events.
      override protected function mouseEventToItemRenderer(event:MouseEvent):IListItemRenderer {
      var row:IListItemRenderer = super.mouseEventToItemRenderer(event);

      if (row && &&‘separator’) &&{
      return null;
      } else {
      return row;

      * Override measuredHeight to maximum possible stageHeightAvailable.
      override protected function measure():void {
      if(!stage || !owner is ComboBox || !dataProvider) return;

      var pt:Point = stage.globalToLocal(owner.localToGlobal(new Point(0, 0)));
      var stageHeightAvailable:Number = Math.max(pt.y,screen.height – pt.y) – (2*rowHeight);
      var calcHeightByItems:Number = measureHeightOfItems()+rowHeight;

      measuredHeight = Math.min(stageHeightAvailable,calcHeightByItems);

      // trace(‘*****height:’+height);
      // trace(‘*****y:’+y);
      // trace(‘*****pt.y:’+pt.y);
      // trace(‘*****screen.y:’+screen.y);
      // trace(‘*****screen.height:’+screen.height);
      // trace(‘*****screen.height-pt.y:’+(screen.height – pt.y));
      // trace(‘*****rowHeight:’+rowHeight);
      // trace(‘*****calcHeightByItems:’+calcHeightByItems);
      // trace(‘*****stageHeightAvailable:’+stageHeightAvailable);
      // trace(‘*****measuredHeight:’+measuredHeight);



  4. @Peter

    That really helped. Two other things I would like to do:
    1. adjust the dropdown height depending on the height of the rows available. (e.g. if I have 2 rows, 1 height=20 and 1 height=60, set to 80, etc.)
    2. adjust the label area height depending on the height of the selected row.


Comments are closed.