Defining a custom CellRenderer class

For example, the following code includes two classes. The ListSample class instantiates a List component, and it uses the other class, CustomRenderer, to define the cell renderer to use for the List component. The CustomRenderer class extends the CellRenderer class.

To use a class that extends the CellRenderer class to define a custom CellRenderer:

  1. Select File > New.
  2. In the New Document dialog box that is displayed, select Flash File (ActionScript 3.0), and then click OK.
  3. Select Window > Components to display the Components panel.
  4. In the Components panel, drag a List component to the Stage.
  5. If Flash is not displaying the Property inspector, select Window > Properties > Properties.
  6. With the List component selected, set the properties in the Property inspector:
    • Instance Name: myList
    • W (width): 200
    • H (height): 300
    • X: 20
    • Y: 20
  7. Select Frame 1 of Layer 1 in the Timeline, and select Window > Actions.
  8. Type the following script in the Actions panel:
    myList.setStyle("cellRenderer", CustomCellRenderer);
    myList.addItem({label:"Burger -- $5.95"});
    myList.addItem({label:"Fries -- $1.95"});
    
  9. Select File > Save. Give the file a name and click the OK button.
  10. Select File > New.
  11. In the New Document dialog box that is displayed, select ActionScript File and then click the OK button.
  12. In the script window, enter the following code to define the CustomCellRenderer class:
package {
    import fl.controls.listClasses.CellRenderer;
    import flash.text.TextFormat;
    import flash.filters.BevelFilter;
    public class CustomCellRenderer2 extends CellRenderer {
        public function CustomCellRenderer2() {
            var format:TextFormat = new TextFormat("Verdana", 12);
            setStyle("textFormat", format);
            this.filters = [new BevelFilter()];
        }
    }
}
  1. Select File > Save. Name the file CustomCellRenderer.as, put in the same directory as the FLA file, and click the OK button.
  2. Select Control > Test Movie.

You can also define a CellRenderer using any class that inherits the DisplayObject class and implements the ICellRenderer interface. For example the following code defines two classes. The ListSample2 class adds a List object to the display list and defines its CellRenderer to use the CustomRenderer class. The CustomRenderer class extends the CheckBox class (which extends the DisplayObject class) and implements the ICellRenderer interface. Note that the CustomRenderer class defines getter and setter methods for the data and listData properties, defined in the ICellRenderer interface. Other properties and methods defined in the ICellRenderer interface (the selected property and the setSize() method) are already defined in the CheckBox class:

To use a class that implements the ICellRenderer interface to define a custom CellRenderer:

  1. Select File > New.
  2. In the New Document dialog box that is displayed, select Flash File (ActionScript 3.0), and then click OK.
  3. Select Window > Components to display the Components panel.
  4. In the Components panel, drag a List component to the Stage.
  5. If Flash is not displaying the Property inspector, select Window > Properties > Properties.
  6. With the List component selected, set the properties in the Property inspector:
    • Instance Name: myList
    • W (width): 100
    • H (height): 300
    • X: 20
    • Y: 20
  7. Select Frame 1 of Layer 1 in the Timeline, and select Window > Actions.
  8. Type the following script in the Actions panel:
    myList.setStyle("cellRenderer", CustomCellRenderer);
    myList.addItem({name:"Burger", price:"$5.95"});
    myList.addItem({name:"Fries", price:"$1.95"});
    
  9. Select File > Save. Give the file a name and click the OK button.
  10. Select File > New.
  11. In the New Document dialog box that is displayed, select ActionScript File and then click the OK button.
  12. In the script window, enter the following code to define the CustomCellRenderer class:
package
{
    import fl.controls.CheckBox;
    import fl.controls.listClasses.ICellRenderer;
    import fl.controls.listClasses.ListData;
    public class CustomCellRenderer extends CheckBox implements ICellRenderer {
        private var _listData:ListData;
        private var _data:Object;
        public function CustomCellRenderer() {
        }
        public function set data(d:Object):void {
            _data = d;
            label = d.label;
        }
        public function get data():Object {
            return _data;
        }
        public function set listData(ld:ListData):void {
            _listData = ld;
        }
        public function get listData():ListData {
            return _listData;
        }
    }
}
  1. Select File > Save. Name the file CustomCellRenderer.as, put in the same directory as the FLA file, and click the OK button.
  2. Select Control > Test Movie.

You can also use a symbol in the library to define a CellRenderer. The symbol must be exported for ActionScript and the class name for the library symbol must have an associated class file that either implements the ICellRenderer interface or that extends the CellRenderer class (or one of its subclasses).

The following example defines a custom CellRenderer using a library symbol.

To use a library symbol to define a CellRenderer:

  1. Select File > New.
  2. In the New Document dialog box that is displayed, select Flash File (ActionScript 3.0), and then click OK.
  3. Select Window > Components to display the Components panel.
  4. In the Components panel, drag a List component to the Stage.
  5. If Flash is not displaying the Property inspector, select Window > Properties > Properties.
  6. With the List component selected, set the properties in the Property inspector:
    • Instance Name: myList
    • W (width): 100
    • H (height): 400
    • X: 20
    • Y: 20
  7. Click the Parameters panel, and then double-click the second column in the dataProvider row.
  8. In the Values dialog box that is displayed, click the plus sign twice to add two data elements (with labels set to label0 and label1), and then click the OK button.
  9. With the Text tool, draw a text field on the Stage.
  10. With the text field selected, set the properties in the Property inspector:
    • Text type: Dynamic Text
    • Instance Name: textField
    • W (width): 100
    • Font size: 24
    • X: 0
    • Y: 0
  11. With the text field selected, select Modify > Convert To Symbol.
  12. In the Convert To Symbol dialog box, make the following settings and then click OK.
    • Name: MyCellRenderer
    • Type: MovieClip
    • Export for ActionScript: Selected
    • Export in First Frame: Selected
    • Class: MyCellRenderer
    • Base Class: flash.display.SimpleButton

    If Flash displays an ActionScript Class Warning, click the OK button in the warning box.

  13. Delete the instance of the new movie clip symbol from the Stage.
  14. Select Frame 1 of Layer 1 in the Timeline, and select Window > Actions.
  15. Type the following script in the Actions panel:
    myList.setStyle("cellRenderer", MyCellRenderer);
    
  16. Select File > Save. Give the file a name and click the OK button.
  17. Select File > New.
  18. In the New Document dialog box that is displayed, select ActionScript File and then click the OK button.
  19. In the script window, enter the following code to define the MyCellRenderer class:
    package {
        import flash.display.MovieClip;
        import flash.filters.GlowFilter;
        import flash.text.TextField;
        import fl.controls.listClasses.ICellRenderer;
        import fl.controls.listClasses.ListData;
        import flash.utils.setInterval;
        public class MyCellRenderer extends MovieClip implements ICellRenderer {
            private var _listData:ListData;
            private var _data:Object;
            private var _selected:Boolean;
            private var glowFilter:GlowFilter;
            public function MyCellRenderer() {
                glowFilter = new GlowFilter(0xFFFF00);
                setInterval(toggleFilter, 200);
            }
            public function set data(d:Object):void {
                _data = d;
                textField.text = d.label;
            }
            public function get data():Object {
                return _data;
            }
            public function set listData(ld:ListData):void {
                _listData = ld;
            }
            public function get listData():ListData {
                return _listData;
            }
            public function set selected(s:Boolean):void {
                _selected = s;
            }
            public function get selected():Boolean {
                return _selected;
            }
            public function setSize(width:Number, height:Number):void {
            }
            public function setStyle(style:String, value:Object):void {
            }
            private function toggleFilter():void {
                if (textField.filters.length == 0) {
                    textField.filters = [glowFilter];
                } else {
                    textField.filters = [];
                }
            }
        }
    }    
    
  20. Select File > Save. Name the file MyCellRenderer.as, put in the same directory as the FLA file, and click the OK button.
  21. Select Control > Test Movie.