Saturday, March 20, 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 (3) [RSS]
 • Adobe (90) [RSS]
 • AdobeMAX06 (45) [RSS]
 • AdobeMAX07 (59) [RSS]
 • AdobeMAX08 (66) [RSS]
 • AdobeMAX09 (39) [RSS]
 • AdobeMAX10 (1) [RSS]
 • AIR (219) [RSS]
 • Appearances (191) [RSS]
 • Books (72) [RSS]
 • CFEclipse (15) [RSS]
 • ColdFusion (1381) [RSS]
 • Data Services (34) [RSS]
 • Fish Tank (5) [RSS]
 • Flash (197) [RSS]
 • Flex (498) [RSS]
 • Home Automation (5) [RSS]
 • Jobs (116) [RSS]
 • JRun (14) [RSS]
 • Labs (43) [RSS]
 • LiveCycle (34) [RSS]
 • MAX (232) [RSS]
 • Mobile (120) [RSS]
 • Regular Expressions (17) [RSS]
 • RIA (21) [RSS]
 • SQL (40) [RSS]
 • Stuff (536) [RSS]
 • Tips (CF Studio) (80) [RSS]
 • Tips (CF) (795) [RSS]
 • Tips (Dreamweaver) (91) [RSS]
 • Tips (Flex Builder) (2) [RSS]
 • Using CF (162) [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 Day : June 8, 2006 / Main
June 8, 2006

MAX 2006 Page Now Online

Registration is not open yet, but the basic MAX 2006 page is now online, and you can subscribe to MAX e-mails so that you'll be the first to know when registration does open and when more details are available.


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>

  © Copyright 1997-2009 Ben Forta, All Rights Reserved