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.

 
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.

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

  1. Farzad says:

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

  2. nice says:

    Uses dataProvider seems doesn’t work.

  3. peterd says:

    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. Rene says:

    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

  5. Gopal says:

    How can we display same tooltip after selecting the item.

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