Flex – dynamic icons part 2 the answer

Filed under: Flex — Wrote by Campbell on Sunday, July 15th, 2007 @ 2:33 am

The answer that I came up with to allow drawing to icons isnt pretty, but it works. And without re-writing your own component it allows you to do what you want.

Events can be used to carry more than just the event string and where the event originated from. It can even carry bitmapdata ;)

The proper alternative to this would be to create your own button class that had a drawToIcon method, which rolled through each of the buttons states icons and drew the bitmapdata to them, but this sort of thing wouldn't be nessesary if a lot of the variables held in the components weren't private instead of protected.

The example can be found HERE and right click to view source.

If you were to use this for a tree component I would actually think about creating your own TreeItemRenderer and drawing to the stored reference of the icon instead.

 

3 Comments   -
  • Comment by kris | May 13, 2009 @ 9:25 am

    hehe this post is getting close to its second birthday – and still there is no better way to put dynamic content to a button. shame on you, adobe. and kudos to you, campbell! :D
    i’m trying to reproduce a similar thing now that i know how to twist it. thanks a lot!

  • Comment by Angus Henderson | June 11, 2010 @ 6:03 am

    This is exactly the problem I am trying to solve, but my icon is on a canvas as a child of a tabnavigator.

    I need to create new canvases at runtime with icons generated from image thumbs. so i think you are saying i should extend canvas and add a drawToIcon method.

    But you set the icon property in the xml…
    icon=”{com.xsive.controls.DynamicIcon}”
    not in actionScript.

    so im not seeing how to set the icon to a class at runtime

  • Comment by Jay | May 24, 2011 @ 3:12 am

    Hi Campbell,
    Thanks for posting this wonderful piece of code, the drawing icons in the buttons is definitely very useful. I need to do the same thing for a Tree structure where dynamically the icons need to be updated without collapsing the tree. Please let me know if you can help me with this.Thanks.

Leave your comment

© Flex developer, Campbell Anderson, from New Zealand – xsive blog -