14
Jul
08

Setting the text roll over color on a ComboBox control in Flex

The following example shows how you can change the text roll over color for a Flex ComboBox control by setting the textRollOverColor style.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/07/14/setting-the-text-roll-over-color-on-a-combobox-control-in-flex/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="top"
        backgroundColor="white">

    <mx:Array id="arr">
        <mx:Object label="One" />
        <mx:Object label="Two" />
        <mx:Object label="Three" />
        <mx:Object label="Four" />
        <mx:Object label="Five" />
        <mx:Object label="Six" />
        <mx:Object label="Seven" />
        <mx:Object label="Eight" />
        <mx:Object label="Nine" />
        <mx:Object label="Ten" />
    </mx:Array>

    <mx:ApplicationControlBar dock="true">
        <mx:Form styleName="plain">
            <mx:FormItem label="textRollOverColor:">
                <mx:ColorPicker id="colorPicker"
                        selectedColor="#FF0000" />
            </mx:FormItem>
        </mx:Form>
    </mx:ApplicationControlBar>

    <mx:ComboBox id="comboBox"
            dataProvider="{arr}"
            textRollOverColor="{colorPicker.selectedColor}" />

</mx:Application>

View source is enabled in the following example.

You can also set the textRollOverColor style in an external .CSS file or <mx:Style /> block, as seen in the following example:

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/07/14/setting-the-text-roll-over-color-on-a-combobox-control-in-flex/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="top"
        backgroundColor="white">

    <mx:Style>
        ComboBox {
            textRollOverColor: red;
        }
    </mx:Style>

    <mx:Array id="arr">
        <mx:Object label="One" />
        <mx:Object label="Two" />
        <mx:Object label="Three" />
        <mx:Object label="Four" />
        <mx:Object label="Five" />
        <mx:Object label="Six" />
        <mx:Object label="Seven" />
        <mx:Object label="Eight" />
        <mx:Object label="Nine" />
        <mx:Object label="Ten" />
    </mx:Array>

    <mx:ComboBox id="comboBox"
            dataProvider="{arr}" />

</mx:Application>

You can also set the textRollOverColor style in ActionScript, as seen in the following example:

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/07/14/setting-the-text-roll-over-color-on-a-combobox-control-in-flex/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="top"
        backgroundColor="white">

    <mx:Script>
        <![CDATA[
            import mx.events.ColorPickerEvent;

            private function colorPicker_change(evt:ColorPickerEvent):void {
                comboBox.setStyle("textRollOverColor", evt.color);
            }
        ]]>
    </mx:Script>

    <mx:Array id="arr">
        <mx:Object label="One" />
        <mx:Object label="Two" />
        <mx:Object label="Three" />
        <mx:Object label="Four" />
        <mx:Object label="Five" />
        <mx:Object label="Six" />
        <mx:Object label="Seven" />
        <mx:Object label="Eight" />
        <mx:Object label="Nine" />
        <mx:Object label="Ten" />
    </mx:Array>

    <mx:ApplicationControlBar dock="true">
        <mx:Form styleName="plain">
            <mx:FormItem label="textRollOverColor:">
                <mx:ColorPicker id="colorPicker"
                        change="colorPicker_change(event);" />
            </mx:FormItem>
        </mx:Form>
    </mx:ApplicationControlBar>

    <mx:ComboBox id="comboBox"
            dataProvider="{arr}" />

</mx:Application>

Due to popular demand, here is the “same” example in a more ActionScript friendly format:

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/07/14/setting-the-text-roll-over-color-on-a-combobox-control-in-flex/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="top"
        backgroundColor="white"
        initialize="init();">

    <mx:Script>
        <![CDATA[
            import mx.containers.ApplicationControlBar;
            import mx.containers.Form;
            import mx.containers.FormItem;
            import mx.controls.ColorPicker;
            import mx.controls.ComboBox;
            import mx.events.ColorPickerEvent;

            private var arr:Array;
            private var colorPicker:ColorPicker;
            private var comboBox:ComboBox;

            private function init():void {
                arr = [];
                arr.push({label:"One"});
                arr.push({label:"Two"});
                arr.push({label:"Three"});
                arr.push({label:"Four"});
                arr.push({label:"Five"});
                arr.push({label:"Six"});
                arr.push({label:"Seven"});
                arr.push({label:"Eight"});
                arr.push({label:"Nine"});
                arr.push({label:"Ten"});

                colorPicker = new ColorPicker();
                colorPicker.addEventListener(ColorPickerEvent.CHANGE, colorPicker_change);

                var formItem:FormItem = new FormItem();
                formItem.label = "textRollOverColor:";
                formItem.addChild(colorPicker);

                var form:Form = new Form();
                form.styleName = "plain";
                form.addChild(formItem);

                var appControlBar:ApplicationControlBar;
                appControlBar = new ApplicationControlBar();
                appControlBar.dock = true;
                appControlBar.addChild(form);
                Application.application.addChildAt(appControlBar, 0);

                comboBox = new ComboBox();
                comboBox.dataProvider = arr;
                addChild(comboBox);
            }

            private function colorPicker_change(evt:ColorPickerEvent):void{
                comboBox.setStyle("textRollOverColor", evt.color);
            }
        ]]>
    </mx:Script>

</mx:Application>

14 Responses to “Setting the text roll over color on a ComboBox control in Flex”


  1. 1 Romz Jul 15th, 2008 at 2:53 am

    Thanx for the example ;-)

  2. 2 Erik Jul 17th, 2008 at 6:28 am

    Ive searched the net for some hours now and cant find any way to get my Combobox in a Plain tex it. I Want my Combobox to be in one color not the upper half more brighter.

    Is there a way to do this? I really hate that I dont have full control over the controls. The only way I see is to make my own component and that really suck and is time consuming.

  3. 3 peterd Jul 17th, 2008 at 7:17 am

    Erik,

    Is this roughly what you’re looking for?

    <?xml version="1.0" encoding="utf-8"?>
    <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
    
        <mx:ComboBox id="comboBox"
                fillColors="[red,red]"
                fillAlphas="[1,1]"
                highlightAlphas="[0,0]"
                dataProvider="[One,Two,Three,Four,Five,Six]" />
    
    </mx:Application>
    

    Peter

  4. 4 Erik Jul 17th, 2008 at 7:38 am

    Yes, Peterd Thank you so much!!! :)

  5. 5 Erik Jul 17th, 2008 at 7:40 am

    Just one more thing I want the borders in the same color, and the line between the arrow and the text removed =) Is this posible?

  6. 6 peterd Jul 17th, 2008 at 9:46 am

    Erik,

    Yes, it’s possible. You’d need to make a new skin graphic/asset in Photoshop/Fireworks/Flash and then set the skin style on the ComboBox.

    Peter

  7. 7 Erik Jul 17th, 2008 at 11:33 pm

    This isn’t possible only with css modifications? I’m going to make the whole design with skining but for the first release I dont have the time to.

    anyway is it posible to remove the borders or set the border color the same as the bakground color in css?

  8. 8 peterd Jul 17th, 2008 at 11:46 pm

    Erik,

    As far as I know, you can’t remove just the borders and the line using just CSS. But I could very well be wrong. You could try asking on FlexCoders and see if anybody knows of a solution.
    Otherwise I think you have to use Flash or some other application to create a graphical skin.

    In fact, I seem to recall that somewhere on the Adobe site is a bunch of Flex skin templates for the various controls/containers. It may be as simple as creating a new skin from the ComboBox template, removing whatever lines you don’t want, publishing a SWC, and then setting the skin style in Flex.

    Peter

  9. 9 Erik Jul 18th, 2008 at 1:27 am

    Thanks for your input Peterd, the fact that I’m going to make the whole design skin based makes me not want to make anything skinbased right now. Butt I will make a try at writing on FlexCoders.

    Thanks again for you help!:)

  10. 10 Ben Aug 1st, 2008 at 2:29 pm

    Hey Peter,

    Do you have any idea how I might go around changing the direction the arrow points on the combobox? I wanted the arrow to point upwards because on my application the ComboBox is actually popping upwards so I feel like it’s only appropriate to have the arrow point upwards.

    Thanks,
    Ben

  11. 11 peterd Aug 1st, 2008 at 5:27 pm

    Ben,

    Off the top of my head I’d say you may need a custom skin. Looking at the API I don’t see an easy way to override the arrow icon. The easiest way is probably to download the “Flex Skin Design Extension for Flash” and create a new skin by going “File > New”, select the “Templates” tab and “Flex Skins” category, and finally select the ComboBox template. For each of the four states, select the down arrow and then select “Modify > Transform > Flip Vertical”. Save and publish the .FLA to generate the .SWC and .SWF file (the .SWC and .SWF will be located in the same directory as your .FLA.)

    Next, in Flex Builder, copy the .SWC file into the /libs/ folder and add this code to the .MXML file:

    <mx:Style>
        ComboBox {
            skin: Embed(skinClass="ComboBox_skin");
        }
    </mx:Style>
    

    Finally, save and run your Flex application and with any luck, you should see the arrow icons pointing upwards.

    There may be a much easier way that I’m not seeing at first glance. You may want to try asking on the FlexCoders list, or even filing an enhancement request in the Flex bug base at http://bugs.adobe.com/flex/.

    Peter

  12. 12 peterd Aug 1st, 2008 at 5:45 pm

    I filed a bug at http://bugs.adobe.com/jira/browse/SDK-16350 and you can grab my source files from there.

    Peter

  13. 13 Ben Aug 4th, 2008 at 9:37 am

    Thanks a bunch, Peter

    That was really helpful. Yea when I was looking into it on Friday I realized that it might have been a skinning issue but I haven’t dealt with skins at all and was having a tough time looking at how to do skinning. Thanks both for your bug submission/change for me and also for the tip on the skin design extension for flash. Very helpful. I’d also like to add thank you for running and maintaining this site. I’ve been using flash for years but just recently started using Flex for my job and this site has been extremely helpful. Keep up the good work!

    Thanks again,
    Ben

  14. 14 John Isaacks May 14th, 2009 at 1:40 pm

    Is there a way to change the roll over text color for the main part of the combo-box not the drop down part? I need the text to change colors because the different skins I am using needs different color text to be readable up/over. I would actually like a global setting as I have the same issue with buttons and other similar components.

    Thanks!

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;".




Badge Farm

  • Powered by Redoable 1.2
  • Cornify
  • Feeds burnt by Feedburner
  • Feed