Flex Examples
Creating a custom icon function on a Flex Tree control
The following example shows how you can build a custom icon function on a Flex Tree control to display different icons depending on the current node name in the data provider.
<?xml version="1.0" encoding="utf-8"?> <!-- http://blog.flexexamples.com/2007/11/15/creating-a-custom-icon-function-on-a-flex-tree-control/ --> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" verticalAlign="middle" backgroundColor="white"> <mx:Script> <![CDATA[ [Bindable] [Embed("assets/bullet_go.png")] private var myBulletGoIcon:Class; [Bindable] [Embed("assets/bullet_star.png")] private var myBulletStarIcon:Class; [Bindable] [Embed("assets/bullet_wrench.png")] private var myBulletWrenchIcon:Class; private function tree_iconFunc(item:Object):Class { var iconClass:Class; switch (XML(item).localName()) { case "league": iconClass = myBulletGoIcon; break; case "division": iconClass = myBulletStarIcon; break; case "team": iconClass = myBulletWrenchIcon; break; } return iconClass; } ]]> </mx:Script> <mx:XML id="dp"> <mlb> <league label="American League"> <division label="East"> <team label="Boston" /> <team label="New York" /> <team label="Toronto" /> <team label="Baltimore" /> <team label="Tampa Bay" /> </division> <division label="Central"> <team label="Cleveland" /> <team label="Detroit" /> <team label="Minnesota" /> <team label="Chicago" /> <team label="Kansas City" /> </division> <division label="West"> <team label="Los Angeles" /> <team label="Seattle" /> <team label="Oakland" /> <team label="Texas" /> </division> </league> </mlb> </mx:XML> <mx:Tree id="TreeProject" dataProvider="{dp.league}" labelField="@label" showRoot="true" iconFunction="tree_iconFunc" width="320" height="240" /> </mx:Application>
View source is enabled in the following example.
Peter deHaan
Peter deHaan currently works for Adobe on the Flex SDK QA team. While not working on Flex, Flash, and ColdFusion applications, Peter enjoys making up bios and writing in 3rd person. Peter's rarely updated blog can be found at blogs.adobe.com/pdehaan/, actionscriptexamples.com, airexamples.com, and coldfusionexamples.com.
-
Add Widgets (Content Sidebar)
This is your Content Sidebar. Edit this content that appears here in the widgets panel by adding or removing widgets in the Content Sidebar area.
10 Responses to Creating a custom icon function on a Flex Tree control
Leave a Reply Cancel reply
-
Categories
- Accordion
- AccordionHeader
- ActionScript
- AddChild
- AdvancedDataGrid
- Alert
- alpha
- Animate
- AnimateProperties
- Application
- Application (Spark)
- ArrayCollection
- BarChart
- baseColor
- beta
- beta1
- beta2
- Bitmap
- Bitmap/BitmapData
- BitmapData
- BitmapFill
- BitmapFill (Spark)
- BitmapGraphic
- BitmapImage
- BitmapImage (Spark)
- BitmapImageResizeMode
- Border (Spark)
- BorderContainer (Spark)
- Box
- BuildInfo
- Button
- Button (Spark)
- ButtonBar
- ButtonBar (Spark)
- ByteArray
- Camera
- Charting
- CheckBox
- CheckBox (Spark)
- ClassFactory
- CollectionEvent
- Color
- ColorPicker
- ColorUtil
- ComboBox
- ComboBoxArrowSkin
- Compiler
- Component
- Component (Spark)
- Configuration
- Container
- ContextMenu
- ContextMenuEvent
- ContextMenuItem
- CSSCondition
- CSSSelector
- CSSStyleDeclaration
- CurrencyFormatter
- CursorManager
- Data Binding
- DataGrid
- DataGrid (Spark)
- DataGridColumn
- Date
- DateBase
- DateChooser
- DateField
- DateFormatter
- Debugging
- DefaultComplexItemRenderer
- DefaultTileListEffect
- DropDownList
- DropDownList (Spark)
- DropDownListButtonSkin
- DropDownListSkin
- DropShadowFilter
- E4X
- Effects
- Ellipse
- EmailValidator
- Embed
- Event
- Fade
- FileFilter
- FileReference
- fill
- Filters
- Flash
- Flash Integration
- FlashVars
- Flex 3 SDK
- Flex Builder
- Flex Builder 3
- Flex SDK
- Flex4
- FLVPlayback
- FocusManager
- FontLookup
- Fonts
- Form
- Form (Spark)
- FormHeading (Spark)
- FormItem
- FormItem (Spark)
- Forms
- FTETextField (Spark)
- FullScreen
- FullScreenEvent
- FxAnimateColor
- FxButtonBar
- FxCheckBox
- FXG
- FxHScrollBar
- FxHSlider
- FxList
- FxNumericStepper
- FxRadioButton
- FxRotate3D
- FxScroller
- FxTextArea
- FxTextInput
- FxToggleButton
- FxVScrollBar
- FxVSlider
- getStyleDeclaration()
- GradientEntry
- Graphic (Spark)
- HBox
- HDividedBox
- HGroup (Spark)
- HorizontalLayout
- HorizontalList
- HSBColor (Spark)
- HScrollBar (Spark)
- HSlider
- HSlider (Spark)
- HTML template
- ID3Info
- Image
- Image (Spark)
- ImageSnapshot
- itemRenderer
- JointStyle
- Label
- Label (Spark)
- Legend
- LegendItem
- LigatureLevel
- Line
- LinearGradientStroke
- LineScaleMode
- LinkBar
- LinkButton
- List
- List (Spark)
- Menu
- MenuBar
- Metadata
- MetadataEvent
- Model
- Mouse
- MouseCursor
- MouseEvent
- Move
- Namespace
- NavigatorContent (Spark)
- needsSWF
- NetConnection
- NetStream
- Nightly Builds
- NumberBaseRoundType
- NumberFormatter
- NumberValidator
- NumericCompare
- NumericStepper
- NumericStepper (Spark)
- ObjectProxy
- ObjectUtil
- paddingLeft
- paddingRight
- Panel
- Panel (Spark)
- Parallel
- Path
- PieChart
- PieSeries
- PieSeriesItem
- PopUpAnchor (Spark)
- PopUpButton
- PopUpManager
- ProgrammaticSkin
- ProgressBar
- PropertyChangeEvent
- QName
- RadialGradient
- RadioButton
- RadioButton (Spark)
- RadioButtonGroup
- RadioButtonGroup (Spark)
- Rect
- RegExp
- Regular Expressions
- Repeater
- RichEditableText
- RichText
- RichText (Spark)
- RichTextEditor
- Rotate
- Rotate3D (Spark)
- Scroller (Spark)
- Sequence
- setStyle()
- SimpleText
- SimpleText (Spark)
- skinClass
- Slider
- SliderEvent
- SolidColor
- SolidColorStroke
- Sort
- SortField
- Sound
- SoundEffect
- Spinner (Spark)
- SpriteVisualElement (Spark)
- StageDisplayState
- States
- StringUtil
- StringValidator
- StyleManager
- Styles
- SWFLoader
- SWFObject
- System
- SystemManager
- TabBar
- TabBar (Spark)
- TabNavigator
- TabStopFormat
- Text
- Text Layout Framework (TLF)
- TextArea
- TextArea (Spark)
- TextBox
- TextConverter
- TextEvent
- TextFlow
- TextFlowUtil
- TextFormat
- TextGraphic
- TextInput
- TextInput (Spark)
- TextLayoutFormat
- TextView
- Themes
- TileLayout
- TileList
- TileOrientation
- Timer
- TitleWindow
- TitleWindow (Spark)
- TLF
- ToggleButton (Spark)
- ToggleButtonBar
- ToolTip
- Transition
- Tree
- TruncationOptions
- UIComponent
- UIFTETextField
- Updater
- URLLoader
- URLRequest
- URLUtil
- URLVariables
- ValidationResultEvent
- Validator
- Validators
- VBox
- VDividedBox
- Vector
- VerticalLayout
- VerticalLayout (Spark)
- VGroup (Spark)
- Video
- VideoDisplay
- VideoElement
- VideoElement (Spark)
- VideoEvent
- VideoPlayer (Spark)
- VideoPlayerScrubBar
- ViewStack
- VScrollBar (Spark)
- VSlider
- VSlider (Spark)
- XML
- XMLList
- XMLListCollection
- ZipCodeValidator
- ZipCodeValidatorDomainType
- Zoom
-
Articles
- December 2010
- November 2010
- October 2010
- September 2010
- August 2010
- July 2010
- June 2010
- May 2010
- April 2010
- March 2010
- February 2010
- January 2010
- December 2009
- November 2009
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
-
Meta


Is there no way the switch case can be made more dynamic? Is there a way to dynamically create the icon class? Because the switch-case forces me to declare every icon class that I would potentially want to use
Great,
Just what I was looking for.. Keep up the good work!! ps. maybee you can have a little lighter color on this blog, so it doesnt feel like a warez site..thanks again for the example..
Hi,
The above example is nice one & I do have a same kind of requirement. But after implementing the above concept I’m finding another issue. I don’t want to display the default ARROW (“>”) icon before the icon images. But once I remove/shade it the tree node expansion does not work. Can you please advice how this can be done?
Many Thanks
Cheers
Partha
Partha,
You can try the following: “Opening branches by clicking rows in a Tree control in Flex”
Peter
Hi Peter,
Thanks for your reply, but unfortunately the above example is not working for my case. My application is bit complex. Let me explain how my application is working:
1. Initially I have a static XML, where most are parent node (0′th level) and few of them has child node (1st level).
2. I call a function from “ItemOpen” property of the tree. This take “TreeEvent”. This function fetch data for 2nd level (if the parent level is 0 or 3rd level data if parent level is 1).
3. Again when, newly populated node (whether it is 2nd or 3rd level) is opened the function retrieve 4th level data…so on.
Now, in your example expansion & collasp is handle by “ListEvent” event type. But same thing I could not replicate in my application where we have “”TreeEvent” as event type.
Not sure, whether I am able to make you understand about the problem or not? But if you have any further more advice then please let me know. It will be great help for me.
Many Thanks
Partha
Hi all.
Thank you for your examples.
But I can’t understand how to make it dinamic.
I find the icon path in a XML document, but I’m not able to attach it to a tree…
Hi all.
I tried this feature but the “Item” value of the function SetIcons is always null.
For information i use a http service to “import” a XML File. So i get always as icon the default icon.
Someone has an idea ?
Thanks.
Nico
Try this:
private function tree_iconFunc(item:Object):Class {
var iconClass:Class;
var icon:String = item.@icon.toString();
switch (icon) {
//some code here
}
return iconClass;
}
thanks a lot for this perfect example
Thank you so much you save my date. it was very helpfull