Thursday, September 02, 2010    
Home My Books Blog ColdFusion About Me Back    

Calendar
<< Jun 2006 >>
S M T W T F S
        1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30  

Search

Categories
 • Acrobat (5) [RSS]
 • Adobe (97) [RSS]
 • AdobeMAX06 (45) [RSS]
 • AdobeMAX07 (59) [RSS]
 • AdobeMAX08 (66) [RSS]
 • AdobeMAX09 (39) [RSS]
 • AdobeMAX10 (11) [RSS]
 • AIR (236) [RSS]
 • Appearances (200) [RSS]
 • Books (81) [RSS]
 • CFEclipse (15) [RSS]
 • ColdFusion (1418) [RSS]
 • ColdFusion Builder (9) [RSS]
 • Data Services (36) [RSS]
 • Fish Tank (5) [RSS]
 • Flash (259) [RSS]
 • Flex (516) [RSS]
 • Home Automation (5) [RSS]
 • Jobs (120) [RSS]
 • JRun (14) [RSS]
 • Labs (47) [RSS]
 • LiveCycle (35) [RSS]
 • MAX (242) [RSS]
 • Mobile (144) [RSS]
 • Regular Expressions (18) [RSS]
 • RIA (21) [RSS]
 • SQL (42) [RSS]
 • Stuff (544) [RSS]
 • Tips (CF Studio) (80) [RSS]
 • Tips (CF) (795) [RSS]
 • Tips (Dreamweaver) (91) [RSS]
 • Tips (Flex Builder) (2) [RSS]
 • Using CF (164) [RSS]

Other BLOGs
 • Charlie Arehart
 • Lee Brimelow
 • Ray Camden
 • Christophe Coenraets
 • Sean Corfield
 • Mihai Corlan
 • Cornel Creanga
 • Mark Doherty
 • John Dowdell
 • Danny Dura
 • Enrique Duvos
 • Steven Erat
 • Kevin Hoyt
 • Serge Jespers
 • Adam Lehman
 • Duane Nickull
 • Miti Pricope
 • Andrew Shorten
 • Ryan Stewart
 • James Ward
 • Greg Wilson
 • Full As A Goog

RSS Feeds
 • Feed
 • Subscribe

Join my mailing list and find out about new books and other topics of interest.

Thoughts, ideas, tips, musings, and pontifications (not necessarily in that order) by Ben Forta ...
NOTE: This is my personal blog, and the opinions and statements voiced here are my own.

Viewing By Entry / Main
June 8, 2006

Creating A Background Color Renderer

Yesterday I need a Flex cell renderer that would change the background color based on the value of the cell (for example, green if a positive number and red if negative). Unfortunately, background color is not a style, that would have been the simplest option.

The solution is to use ActionScript drawing APIs, and the following code was given to me by Flex Evangelist Christophe Coenraets. I am posting it here in case anyone else needs it, and so that I'll have it around when I need it next.

First the renderer:

package {
    import mx.controls.Label;
    import mx.controls.dataGridClasses.*;
    import mx.controls.DataGrid;
    import flash.display.Graphics;

    public class BackgroundColorRenderer extends Label
    {
        override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
        {
            super.updateDisplayList(unscaledWidth, unscaledHeight);

            var g:Graphics = graphics;
            var grid:DataGrid = DataGrid(DataGridListData(listData).owner);

            if (data.balance >
= 0)
            {
                g.beginFill(0x009900);
                g.drawRect(0, 0, unscaledWidth, unscaledHeight);
                g.endFill();
            }
            else
            {
                g.beginFill(0xFF0000);
                g.drawRect(0, 0, unscaledWidth, unscaledHeight);
                g.endFill();
            }
        }
    }
}

And here is an example of the renderer being used:

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" height="400" width="600">

    <mx:Script>
    <![CDATA[
        [Bindable]
        public var myDP:Array =
        [
            {name:"Andy", balance:-400},
            {name:"Bill", balance:100},
            {name:"Jim", balance:350},
            {name:"John", balance:500},
            {name:"Steve", balance:-600},
        ]
    ]]>

    </mx:Script>

    <mx:DataGrid id="myDG" dataProvider="{myDP}">
        <mx:columns>
            <mx:DataGridColumn dataField="name" headerText="Name" itemRenderer="BackgroundColorRenderer" />
            <mx:DataGridColumn dataField="balance" headerText="Balance" itemRenderer="BackgroundColorRenderer" />
        </mx:columns>
    </mx:DataGrid>

</mx:Application>

Comments
This is great when you want to change the background color of the specific cell, but what if instead you wish to set it for the entire row? In the renderer, one can access the row in question via this.listData.rowIndex, but I'm not sure where to go from there. Any ideas?
# Posted By Steve | 6/17/06 9:49 PM
backgroundColor is not, for some strange reason, available as a style, but it is as a property.

If you look in the inherited properties of the dataGridItemRenderer you will see it.

just do
background=true;
backgroundColor=0xFF0000;

inside your renderer.

Cheers.
# Posted By Paul | 8/10/06 6:32 AM
Hey,

Cool Site...I have learned a bunkch of stuff here and from a few other bloggers. I am not really sure where to ask this question since resources are kind of limited on Flex. So I thought I'd ask you.

I am working with the Dashboard Demo app from the Adobe Demo Apps website ("http://www.adobe.com/devnet/flex/?tab:samples=1&qu...;). I am trying to add in an extra column with colored Alarms (basically the background color for an individual cell changes depending on the value <positive or negative>.

I am using a custom renderer that I found here ("http://www.forta.com/blog/index.cfm/2006/6/8/Creat...;)

When I run the code for the custom renderer in a sandbox all is well. But when I add the code to the Dashboard App I run into errors.

I am comming from php/javascipt and before the .net/ASP/VB world so I am having trouble debugging the errors.

Any ideas you have would be appreciated....
# Posted By billp | 4/3/07 6:12 PM
I have solved the issue. Thanks to your site I got going on the right track. I have another issue and I thought you might be able to help.

Much like the DataGrid Background Renderer I also need a TabNavigator header colors. I have searched for this but there does not seem to be any way change these properties for each header.

You can only change based on Selected on not Selected. I would like to access each and change based on Bus Logic. Any Ideas...
# Posted By billp | 4/12/07 6:42 PM

  © Copyright 1997-2009 Ben Forta, All Rights Reserved