Displaying item tool tips in a Flex ComboBox control

by Peter deHaan on January 26, 2008

in ComboBox

The following example shows how you can truncate long item labels in a ComboBox control by setting a custom item renderer. In this example we’re using the Label control we’re using the Label control and taking advantage of the Label control’s truncateToFit property which truncates the string with an “…” and displays a tool tip showing the entire, non-truncated string when the truncateToFit property is set to true (which is the default value).

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/01/26/displaying-item-tool-tips-in-a-flex-combobox-control/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="top"
        backgroundColor="white">

    <mx:Array id="arr">
        <mx:Object label="Lorem ipsum dolor sit amet, consectetuer adipiscing elit." />
        <mx:Object label="Donec sit amet dui nec pede aliquam auctor." />
        <mx:Object label="Integer vestibulum sodales dui." />
        <mx:Object label="Sed nonummy ligula et tortor." />
        <mx:Object label="Aenean varius neque vel felis." />
        <mx:Object label="Phasellus venenatis ipsum sit amet nisi." />
        <mx:Object label="Nullam vitae turpis et ipsum cursus venenatis." />
        <mx:Object label="Pellentesque tincidunt pede non arcu." />
        <mx:Object label="Aliquam ut massa quis ante dignissim egestas." />
        <mx:Object label="Curabitur facilisis velit sit amet metus." />
        <mx:Object label="Vivamus ornare massa ac massa." />
        <mx:Object label="Nam accumsan rutrum purus." />
    </mx:Array>

    <mx:ComboBox id="cb"
            dataProvider="{arr}"
            itemRenderer="mx.controls.Label"
            width="200" />

</mx:Application>

View source is enabled in the following example.

I didn’t think this was interesting (or useful) enough to warrant a new entry, so I’ll just post it here. The following example shows how you can set a tool tip on both the ComboBox control as well as its dropdown menu. The ComboBox control on the left only sets the toolTip property, so the tool tip only displays while over the ComboBox itself, not when the mouse cursor is over the dropdown menu. The ComboBox control on the right sets the toolTip property in MXML and then uses the creationComplete event to call a method which sets the ComboBox control’s dropdown.toolTip property to the ComboBox control’s toolTip property.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/01/26/displaying-item-tool-tips-in-a-flex-combobox-control/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="horizontal"
        verticalAlign="top"
        backgroundColor="white">

    <mx:Script>
        <![CDATA[
            private function init():void {
                comboBox2.dropdown.toolTip = comboBox2.toolTip;
            }
        ]]>
    </mx:Script>

    <mx:Array id="arr">
        <mx:Object label="Lorem ipsum dolor sit amet, consectetuer adipiscing elit." />
        <mx:Object label="Donec sit amet dui nec pede aliquam auctor." />
        <mx:Object label="Integer vestibulum sodales dui." />
        <mx:Object label="Sed nonummy ligula et tortor." />
        <mx:Object label="Aenean varius neque vel felis." />
        <mx:Object label="Phasellus venenatis ipsum sit amet nisi." />
        <mx:Object label="Nullam vitae turpis et ipsum cursus venenatis." />
        <mx:Object label="Pellentesque tincidunt pede non arcu." />
        <mx:Object label="Aliquam ut massa quis ante dignissim egestas." />
        <mx:Object label="Curabitur facilisis velit sit amet metus." />
        <mx:Object label="Vivamus ornare massa ac massa." />
        <mx:Object label="Nam accumsan rutrum purus." />
    </mx:Array>

    <mx:ComboBox id="comboBox1"
            dataProvider="{arr}"
            toolTip="Please select some text."
            width="200" />

    <mx:ComboBox id="comboBox2"
            dataProvider="{arr}"
            toolTip="Please select some text."
            width="200"
            creationComplete="init();" />

</mx:Application>

View source is enabled in the following example.

{ 7 comments… read them below or add one }

1 Chandra Kumar July 4, 2008 at 7:55 am

I tried the first example on this page using “mx.controls.Text” for the “itemRenderer” property of ComboBox. I neither got a tooltip nor found the contents to be wrapped around. I am learning Flex. Your comments would be helpful to me.

Reply

2 peterd July 4, 2008 at 2:18 pm

Chandra Kumar,

Try something like the following instead:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="top"
        backgroundColor="white">

    <mx:Array id="arr">
        <mx:Object label="Lorem ipsum dolor sit amet, consectetuer adipiscing elit." />
        <mx:Object label="Donec sit amet dui nec pede aliquam auctor." />
        <mx:Object label="Integer vestibulum sodales dui." />
        <mx:Object label="Sed nonummy ligula et tortor." />
        <mx:Object label="Aenean varius neque vel felis." />
        <mx:Object label="Phasellus venenatis ipsum sit amet nisi." />
        <mx:Object label="Nullam vitae turpis et ipsum cursus venenatis." />
        <mx:Object label="Pellentesque tincidunt pede non arcu." />
        <mx:Object label="Aliquam ut massa quis ante dignissim egestas." />
        <mx:Object label="Curabitur facilisis velit sit amet metus." />
        <mx:Object label="Vivamus ornare massa ac massa." />
        <mx:Object label="Nam accumsan rutrum purus." />
    </mx:Array>

    <mx:ComboBox id="cb"
            dataProvider="{arr}"
            width="250"
            open="cb.dropdown.variableRowHeight = true;">
        <mx:itemRenderer>
            <mx:Component>
                <mx:Text selectable="false" toolTip="{data.label}" />
            </mx:Component>
        </mx:itemRenderer>
    </mx:ComboBox>

</mx:Application>

Peter

Reply

3 Chandra Kumar July 5, 2008 at 2:29 am

Peter,
Thank you. I followed the solution given by you. It works fine.

Chandra Kumar

Reply

4 Chandra Kumar July 7, 2008 at 5:26 am

Peter,
I wonder how mx.controls.TextInput control would be used as an item renderer for mx.controls.ComboBox control.

Chandra Kumar

Reply

5 rosen jiang November 10, 2008 at 2:54 am

Well, if some one want create ComboBox by Constructor. Please use:

var combo:ComboBox = new ComboBox();
combo.x = 3;
combo.y = 40;
combo.width = 138;
combo.itemRenderer = new ClassFactory(mx.controls.Label);
……..

best,
rosen

Reply

6 sabeerdeen December 13, 2009 at 7:47 pm

how to make arraylist items as tool tip for combobox items?
arraylist items[one ,two ,three, four, five]
combobox items[1,2,3,4,5]
eg:
if combobox item is 1 ,then the tool tip should be one
if combobox item is 2 ,then the tool tip should be two

Reply

7 Peter deHaan December 13, 2009 at 11:10 pm

@sabeerdeen,

I’d probably just listen for the change event on the ComboBox control and dynamically set the toolTip property based on the currently selected item.

Peter

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: