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

by Peter deHaan on September 22, 2007

in ComboBox, Label

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 comments… read them below or add one }

1 Farzad October 4, 2007 at 1:42 pm

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

Reply

2 nice February 4, 2008 at 2:40 pm

Uses dataProvider seems doesn’t work.

Reply

3 peterd February 4, 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

Reply

4 Rene February 20, 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

Reply

Leave a Comment

Sorry, this blog is terrible at eating HTML comments.
If you're pasting any HTML/XML/MXML code, you need to convert your < characters to &lt; and your > characters to &gt; .

You can 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

Previous post:

Next post: