<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Blue Flex &#187; form</title>
	<atom:link href="http://kennethsutherland.com/tag/form/feed/" rel="self" type="application/rss+xml" />
	<link>http://kennethsutherland.com</link>
	<description>Flex with a hint of cool</description>
	<lastBuildDate>Wed, 18 Apr 2012 21:22:11 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.4</generator>
		<item>
		<title>FormItem, adding an icon</title>
		<link>http://kennethsutherland.com/2009/05/27/formitem-adding-an-icon/</link>
		<comments>http://kennethsutherland.com/2009/05/27/formitem-adding-an-icon/#comments</comments>
		<pubDate>Wed, 27 May 2009 13:07:13 +0000</pubDate>
		<dc:creator>Kenneth</dc:creator>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[extend]]></category>
		<category><![CDATA[form]]></category>
		<category><![CDATA[formItem]]></category>
		<category><![CDATA[image]]></category>
		<category><![CDATA[override]]></category>

		<guid isPermaLink="false">http://kennethsutherland.com/?p=425</guid>
		<description><![CDATA[Adding an icon to a form item is (yet another) one of those really annoying things in flex. I&#8217;m sure its something that loads of people wish to do but you can&#8217;t. Well just to see if I could, I set about and extended the FormItem class so that I could add an image/icon. After [...]]]></description>
			<content:encoded><![CDATA[<p>Adding an icon to a form item is (yet another) one of those really annoying things in flex.  I&#8217;m sure its something that loads of people wish to do but you can&#8217;t.</p>
<p>Well just to see if I could, I set about and extended the FormItem class so that I could add an image/icon.</p>
<p>After a quick look at the source code of the FormItem you can see that it only has two children. One, the label and two, the indicator.  So if you do wish to add anything else you&#8217;re going to have to extend the FormItem.</p>
<p>Thankfully the FormItem is based on the Container which makes adding anything fairly straight forward. I prefer to do any extending in actionscript but you could do the below in MXML with a bit of actionscript code at the same time.</p>
<p>There are 3 steps you need to make.</p>
<ol>
<li>Override the createChildren method</li>
<li>Set the image source</li>
<li>Override the label</li>
</ol>
<h4>Override the createChildren</h4>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">protected</span> override <span style="color: #339966; font-weight: bold;">function</span> createChildren<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
    <span style="color: #0033ff; font-weight: bold;">super</span><span style="color: #000066; font-weight: bold;">.</span>createChildren<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
    <span style="color: #009900; font-style: italic;">//you could move the image creation into a seperate function when and if the imagesource has been set</span>
	<span style="color: #009900; font-style: italic;">//but for this example I've kept it simple.</span>
	<span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">.</span>image = <span style="color: #0033ff; font-weight: bold;">new</span> Image<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	image<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">width</span> = <span style="color: #000000; font-weight:bold;">16</span><span style="color: #000066; font-weight: bold;">;</span>
	image<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">height</span> = <span style="color: #000000; font-weight:bold;">16</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #009900; font-style: italic;">//again I've hardcoded these values for simplicity</span>
	<span style="color: #009900; font-style: italic;">//You could if you wanted to create a versitile custom component load these values in from a CSS file</span>
	image<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">setStyle</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">'verticalCenter'</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">0</span> <span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	image<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">setStyle</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">'left'</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">5</span> <span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	image<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">source</span> = _imageSource<span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">.</span>rawChildren<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>image<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #009900; font-style: italic;">//bind the string property to the image source property.</span>
	BindingUtils<span style="color: #000066; font-weight: bold;">.</span>bindProperty<span style="color: #000000;">&#40;</span>image<span style="color: #000066; font-weight: bold;">,</span> <span style="color: #990000;">'source'</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #990000;">'imageSource'</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>from the above code you&#8217;ll see that all I&#8217;ve done is create an image, set its various properties and add it to the form.</p>
<p>most of this could and probably should be moved to a separate function (but for this demo I haven&#8217;t) so that you only add the image if it is actually required.  Also the style settings should come from a CSS file but for simplicity of the demo I haven&#8217;t done this.</p>
<h4>Set the image source</h4>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> _imageSource <span style="color: #000066; font-weight: bold;">:</span> <span style="color: #004993;">String</span> = <span style="color: #990000;">''</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
<span style="color: #000000;">&#91;</span>Bindable<span style="color: #000000;">&#93;</span>
<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #0033ff; font-weight: bold;">get</span> imageSource<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">:</span> <span style="color: #004993;">String</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">return</span> _imageSource<span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span> 
&nbsp;
<span style="color: #009900; font-style: italic;">//Sets the imageSource and I've added a number of spaces at the start to offset the </span>
<span style="color: #009900; font-style: italic;">//width of the image.</span>
<span style="color: #009900; font-style: italic;">//The overall form width will be calculated from the width of the label (this is done inside the FormItem) </span>
<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #0033ff; font-weight: bold;">set</span> imageSource<span style="color: #000000;">&#40;</span>str <span style="color: #000066; font-weight: bold;">:</span> <span style="color: #004993;">String</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">:</span> <span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
  _imageSource = str<span style="color: #000066; font-weight: bold;">;</span>
  <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>_imageSource<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">length</span> <span style="color: #000066; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><span style="color: #009900; font-style: italic;">//setting the label (not using _label) will resize the form/formItems</span>
  <span style="color: #009900; font-style: italic;">//add spaces to the trimed version to make sure you don't end up with 100's of spaces at the start.</span>
	label = <span style="color: #990000;">&quot;   &quot;</span> <span style="color: #000066; font-weight: bold;">+</span> StringUtil<span style="color: #000066; font-weight: bold;">.</span>trim<span style="color: #000000;">&#40;</span>_label<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
  <span style="color: #000000;">&#125;</span> <span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #000000;">&#123;</span>
	label = StringUtil<span style="color: #000066; font-weight: bold;">.</span>trim<span style="color: #000000;">&#40;</span>_label<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>In the above code you can see that I set the variable _imageSource (which bound to the images source &#8211; see first snippet of code).  If the source is not &#8220;&#8221; then I add spaces to the label, make sure you set label and not _label.  This makes sure that the label width gets recalcualted.</p>
<h4>Override the label</h4>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #009900; font-style: italic;">//Sets the label.</span>
<span style="color: #009900; font-style: italic;">//If the imageSource has been set already then this will add spaces to the label</span>
<span style="color: #0033ff; font-weight: bold;">public</span> override <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #0033ff; font-weight: bold;">set</span> label<span style="color: #000000;">&#40;</span>str <span style="color: #000066; font-weight: bold;">:</span> <span style="color: #004993;">String</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">:</span> <span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
  _label = str<span style="color: #000066; font-weight: bold;">;</span>
  <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>_imageSource<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">length</span> <span style="color: #000066; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
	_label = <span style="color: #990000;">&quot;   &quot;</span> <span style="color: #000066; font-weight: bold;">+</span> str<span style="color: #000066; font-weight: bold;">;</span>
  <span style="color: #000000;">&#125;</span> <span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #000000;">&#123;</span>
	_label = StringUtil<span style="color: #000066; font-weight: bold;">.</span>trim<span style="color: #000000;">&#40;</span>str<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
  <span style="color: #000000;">&#125;</span>
  <span style="color: #009900; font-style: italic;">// call the super last, this will also force the remeausing of the formItem and Form</span>
  <span style="color: #0033ff; font-weight: bold;">super</span><span style="color: #000066; font-weight: bold;">.</span>label = _label<span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>The above code is very similar to the setter that set the imageSource but this time it sets _label then makes sure that the super function gets the new label.</p>
<p>Check out the <a title="FormItem demo" href="http://kennethsutherland.com/flex/formItem/FormItemDemo.html" target="_blank">simple demo here</a>. (Right click for source)</p>
<p style="text-align: center;"><script type="text/javascript"><!--
google_ad_client = "pub-7396620608505330";
google_ad_slot = "1277482570";
google_ad_width = 468;
google_ad_height = 60;
//--></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
</p>
]]></content:encoded>
			<wfw:commentRss>http://kennethsutherland.com/2009/05/27/formitem-adding-an-icon/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

