22
Sep
07

Truncating labels in a Flex ComboBox dropdown menu using an item renderer

The following example shows how you can use a Label control as an item renderer in a Flex ComboBox control so that items which exceed the combo box dropdown menu’s width are truncated with a nice “…”.

Full code after the jump

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2007/09/22/truncating-labels-in-a-flex-combobox-dropdown-menu-using-an-item-renderer/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="top"
        backgroundColor="white">

    <mx:ApplicationControlBar dock="true">
        <mx:Label text="width:" />
        <mx:HSlider id="slider"
                minimum="50"
                maximum="200"
                value="80"
                liveDragging="true"
                labels="[50,200]"
                tickInterval="50"
                snapInterval="10" />
    </mx:ApplicationControlBar>

    <mx:ComboBox id="comboBox"
            width="{slider.value}"
            itemRenderer="mx.controls.Label">
        <mx:dataProvider>
            <mx:Array>
                <mx:Object label="1234567890" />
                <mx:Object label="The quick brown fox" />
                <mx:Object label="Lorem ipsum" />
            </mx:Array>
        </mx:dataProvider>
    </mx:ComboBox>

</mx:Application>

View source is enabled in the following example.


4 Responses to “Truncating labels in a Flex ComboBox dropdown menu using an item renderer”


  1. 1 Farzad Oct 4th, 2007 at 1:42 pm

    this is great, ODD that i have to call mx.control.label, instead of using trunicate=”on”
    but great !!

  2. 2 nice Feb 4th, 2008 at 2:40 pm

    Uses dataProvider seems doesn’t work.

  3. 3 peterd Feb 4th, 2008 at 4:05 pm

    nice,

    It is working for me using Flex 201 Hotfix 3 and the latest Flex 3 nightly build. What is the error that you are getting?

    Peter

  4. 4 Rene Feb 20th, 2008 at 4:27 pm

    good idea,
    However Label Renderer does not support icons anymore.
    Is there a solution (an itemRenderer ?) that combines icons with the truncate/tool-tip feature of Labels ?

    Rene

Leave a Reply

This blog is terrible at eating HTML tags. If you plan on posting code/XML, please escape your "<" characters as "&lt;" and your ">" characters as "&gt;".