Automatic Disabled Icon with Flex Button Control

The Flex Button control provides the means to add an icon to the button, in one of several states disabled/hover etc. However it does not automatically provide a disabled version of the icon you add for its normal enabled state. i.e. A “grayed out” version, which most other visual RAD tools would provided.

After hacking around in the Flex SDK, I found the following code which accomplishes this;

// Fade Disabled Icon

DisplayObject(this.getChildByName("disabledIcon")).alpha = 0.4;

This could be used as follows to produce an EditButton class;

<?xml version="1.0" encoding="utf-8"?>

<mx:Button xmlns:mx="http://www.adobe.com/2006/mxml"
	icon="@Embed('EditButton.png')"
	label="Edit" creationComplete="init();"
>
        <mx:Metadata>
		[IconFile("EditButton.png")]
	</mx:Metadata>

	<mx:Script>

 	<![CDATA[

		private function init():void
		{
    			// Fade Disabled Icon
			DisplayObject(this.getChildByName("disabledIcon")).alpha = 0.4;
		}

	]]>

 	</mx:Script>

</mx:Button>

As I believe this to be a very valuable behavior, I have created a new IconButton Flex Component class to download, which provides a Button control which does automatically provided a disabled version of its main icon.


2 Responses to “Automatic Disabled Icon with Flex Button Control”

  1. Glen Black Says:

    This is a neat trick; thanks for publishing it.

    At runtime, I had immediately received “TypeError: Error #1009: Cannot access a property or method of a null object reference.” on the line that sets the alpha in the IconButton class.

    It turns out that if I have a button whose default initial state is enabled, the error occurs. If I make the button’s default initial state disabled and enable the button later, the error goes away; that’s certainly an easy enough workaround for my needs.

    I hope this helps someone if they run into the same issue.

    Thanks
    [gb]

  2. David Says:

    @Glen

    Thanks for the comment.

    I have updated the IconButton component to fix this issue.


Leave a Reply

Copyright © 2005, David Beale

  • Valid XHTML 1.0!
  • Valid CSS
  • Level Triple-A conformance icon, W3C-WAI Web Content Accessibility Guidelines 1.0