<?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>Zigron Inc. Blog &#187; Faisal Jameel</title>
	<atom:link href="http://www.zigron.com/blog/author/admin/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.zigron.com/blog</link>
	<description></description>
	<lastBuildDate>Sat, 24 Jul 2010 03:28:31 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Image Selection in JSF</title>
		<link>http://www.zigron.com/blog/2008/06/18/image-selection-in-jsf/</link>
		<comments>http://www.zigron.com/blog/2008/06/18/image-selection-in-jsf/#comments</comments>
		<pubDate>Wed, 18 Jun 2008 10:50:45 +0000</pubDate>
		<dc:creator>Faisal Jameel</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Zigron Inc]]></category>
		<category><![CDATA[Image Selection Component in JSF]]></category>
		<category><![CDATA[Image Selection in JSF]]></category>
		<category><![CDATA[JSF Image Selection]]></category>

		<guid isPermaLink="false">http://www.zigron.com/blog/?p=41</guid>
		<description><![CDATA[JSF is really nice, but developers sometimes feel suffocated because there aren’t enough components. Recently I ran into a problem of displaying a list of images in grid or you could say in form of rows and columns and being able to select one of them using radio buttons. I searched on the internet but [...]]]></description>
			<content:encoded><![CDATA[<p>JSF is really nice, but developers sometimes feel suffocated because there aren’t enough components. Recently I ran into a problem of displaying a list of images in grid or you could say in form of rows and columns and being able to select one of them using radio buttons. I searched on the internet but couldn’t find any component that gave the desired functionality. So our team came up with an elegant extensible solution that I feel that I should share with the world since human knowledge belongs to the world.</p>
<p style="text-align: center;" align="center"><span style="text-decoration: none;">&lt;!&#8211;[if !vml]&#8211;&gt;<span><img class="alignnone size-medium wp-image-5 aligncenter" src="http://richfaces.wordpress.com/files/2008/06/image-list.jpg?w=300" border="0" alt="" width="300" height="92" /></span>&lt;!&#8211;[endif]&#8211;&gt;</span></p>
<p>The JSP code to achieve the results of figure looks like:-</p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: ">1 &lt;</span><span style="font-size: 10pt; font-family: ">div </span><span style="font-size: 10pt; font-family: ">style</span><span style="font-size: 10pt; font-family: ">=</span><span style="font-size: 10pt; font-family: ">&#8220;width:620px;height:270px; overflow:auto;&#8221;</span><span style="font-size: 10pt; font-family: ">&gt;</span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: ">2<span> </span>&lt;</span><span style="font-size: 10pt; font-family: ">a4j:repeat<span> </span></span><span style="font-size: 10pt; font-family: ">value</span><span style="font-size: 10pt; font-family: ">=</span><span style="font-size: 10pt; font-family: ">&#8220;<span style="color: black;">#{controller.imageList}</span>&#8220;<span style="color: black;"> </span><span style="color: #7f007f;">var</span><span style="color: black;">=</span><span style="color: #2a00ff;">&#8220;imageList&#8221;</span><span style="color: teal;">&gt;</span></span></p>
<p class="MsoNormal"> </p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: ">3<span> </span>&lt;</span><span style="font-size: 10pt; font-family: ">div<span> </span></span><span style="font-size: 10pt; font-family: ">style</span><span style="font-size: 10pt; font-family: ">=</span><span style="font-size: 10pt; font-family: ">&#8220;width:85px;float:left;&#8221;</span><span style="font-size: 10pt; font-family: ">&gt;</span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: ">4<span> </span></span><span style="font-size: 10pt; font-family: ">&lt;</span><span style="font-size: 10pt; font-family: ">h:graphicImage </span><span style="font-size: 10pt; font-family: ">style</span><span style="font-size: 10pt; font-family: ">=</span><span style="font-size: 10pt; font-family: ">&#8220;border:2px solid #ccc;&#8221; </span><span style="font-size: 10pt; font-family: ">height</span><span style="font-size: 10pt; font-family: ">=</span><span style="font-size: 10pt; font-family: ">&#8220;43&#8243; </span><span style="font-size: 10pt; font-family: ">width</span><span style="font-size: 10pt; font-family: ">=</span><span style="font-size: 10pt; font-family: ">&#8220;57&#8243; </span><span style="font-size: 10pt; font-family: ">url</span><span style="font-size: 10pt; font-family: ">=</span><span style="font-size: 10pt; font-family: ">&#8220;<span style="color: black;">#{imageList[0]}</span>&#8220;<span style="color: teal;">&gt;&lt;/</span><span style="color: #3f7f7f;">h:graphicImage</span><span style="color: teal;">&gt;</span></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: ">5<span> </span></span><span style="font-size: 10pt; font-family: ">&lt;</span><span style="font-size: 10pt; font-family: ">h:selectOneRadio<span> </span></span><span style="font-size: 10pt; font-family: ">value</span><span style="font-size: 10pt; font-family: ">=</span><span style="font-size: 10pt; font-family: ">&#8220;<span style="color: black;">#{ imageList[1]}</span>&#8220;<span style="color: black;"> </span><span style="color: #7f007f;">layout</span><span style="color: black;">=</span><span style="color: #2a00ff;">&#8220;lineDirection&#8221;</span><span style="color: teal;">&gt;</span></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: ">6<span> </span></span><span style="font-size: 10pt; font-family: ">&lt;</span><span style="font-size: 10pt; font-family: ">f:selectItem </span><span style="font-size: 10pt; font-family: ">itemLabel</span><span style="font-size: 10pt; font-family: ">=</span><span style="font-size: 10pt; font-family: ">&#8220;&#8221; </span><span style="font-size: 10pt; font-family: ">itemValue</span><span style="font-size: 10pt; font-family: ">=</span><span style="font-size: 10pt; font-family: ">&#8220;selImage&#8221;</span><span style="font-size: 10pt; font-family: ">/&gt;</span></p>
<p class="MsoNormal" style="margin-left: 1.5in; text-indent: -1.5in;"><span style="font-size: 10pt; font-family: ">7<span> </span></span><span style="font-size: 10pt; font-family: ">&lt;</span><span style="font-size: 10pt; font-family: ">a4j:support </span><span style="font-size: 10pt; font-family: ">event</span><span style="font-size: 10pt; font-family: ">=</span><span style="font-size: 10pt; font-family: ">&#8220;onclick&#8221; </span><span style="font-size: 10pt; font-family: ">action</span><span style="font-size: 10pt; font-family: ">=</span><span style="font-size: 10pt; font-family: ">&#8220;<span style="color: black;">#{controller.selectedImageRenderer}</span>&#8220;&gt;</span></p>
<p class="MsoNormal" style="margin-left: 0.5in; text-indent: -0.5in;"><span style="font-size: 10pt; font-family: ">8<span> </span></span><span style="font-size: 10pt; font-family: ">&lt;</span><span style="font-size: 10pt; font-family: ">f:param </span><span style="font-size: 10pt; font-family: ">name</span><span style="font-size: 10pt; font-family: ">=</span><span style="font-size: 10pt; font-family: ">&#8220;selectedImageId&#8221; </span><span style="font-size: 10pt; font-family: ">value</span><span style="font-size: 10pt; font-family: ">=</span><span style="font-size: 10pt; font-family: ">&#8220;<span style="color: black;">#{ imageList[2]}</span>&#8220;<span style="color: black;"> </span><span style="color: teal;">/&gt;</span></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: ">&lt;/</span><span style="font-size: 10pt; font-family: ">a4j:support</span><span style="font-size: 10pt; font-family: ">&gt;</span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: "></span><span style="font-size: 10pt; font-family: ">&lt;/</span><span style="font-size: 10pt; font-family: ">h:selectOneRadio</span><span style="font-size: 10pt; font-family: ">&gt;</span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: ">&lt;/</span><span style="font-size: 10pt; font-family: ">div</span><span style="font-size: 10pt; font-family: ">&gt;</span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: ">&lt;/</span><span style="font-size: 10pt; font-family: ">a4j:repeat</span><span style="font-size: 10pt; font-family: ">&gt;</span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: ">&lt;/</span><span style="font-size: 10pt; font-family: ">div</span><span style="font-size: 10pt; font-family: ">&gt;</span></p>
<p class="MsoNormal"> </p>
<p><strong>At line 1</strong>, The outer divs width is responsible for number of images appearing in a row. The outer divs height is responsible for number images appearing in columns after which the scroll bar shall appear. You are most welcome to play with the div to set number of images in rows/columns.<br />
<strong>At line 2</strong>, the a4j repeat tag repeats through a list of images which was populated at the controller.<br />
<strong>At line 3</strong>, we see another div whom we assigned the spacing responsibilities between the images.<br />
<strong>At line 4</strong>, the graphic image in the list is displayed. The zero index of the list contains the url to the image.<br />
<strong>At line 5</strong>, radio button is displayed for selecting an image, the value to this radio button is assigned by the first index of the imageList.</p>
<p><strong>At line 6,</strong> we define the selection criteria, basically whichever radio button element shall contain “selImage”, it will be selected.<br />
<strong>At line 7</strong>, when the event click happens on the radio button the method selectedImageRender is called, which we assigned the responsibilities of selecting an image and cleaning up the rest of the radio buttons.<span> </span>imageList[2] contains the currently selected image Id.</p>
<p>The code for the method is fairly simple and is written as follows:-</p>
<p class="MsoNormal" style="text-indent: 0.0in; text-align: left;"><span style="font-size: 10pt; font-family: ">/***</span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: "></span><span style="font-size: 10pt; font-family: ">*</span><span style="font-size: 10pt; font-family: "> </span><strong><span style="font-size: 10pt; font-family: ">@author</span></strong><span style="font-size: 10pt; font-family: "> </span><span style="font-size: 10pt; font-family: ">waqas.rajab</span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: "></span><span style="font-size: 10pt; font-family: ">*</span><span style="font-size: 10pt; font-family: "> </span><span style="font-size: 10pt; font-family: ">This</span><span style="font-size: 10pt; font-family: "> </span><span style="font-size: 10pt; font-family: ">method</span><span style="font-size: 10pt; font-family: "> </span><span style="font-size: 10pt; font-family: ">makes</span><span style="font-size: 10pt; font-family: "> </span><span style="font-size: 10pt; font-family: ">the</span><span style="font-size: 10pt; font-family: "> </span><span style="font-size: 10pt; font-family: ">selection</span><span style="font-size: 10pt; font-family: "> </span><span style="font-size: 10pt; font-family: ">of</span><span style="font-size: 10pt; font-family: "> </span><span style="font-size: 10pt; font-family: ">the</span><span style="font-size: 10pt; font-family: "> </span><span style="font-size: 10pt; font-family: ">appropriate</span><span style="font-size: 10pt; font-family: "> </span><span style="font-size: 10pt; font-family: ">image</span><span style="font-size: 10pt; font-family: "> </span><span style="font-size: 10pt; font-family: ">selected</span><span style="font-size: 10pt; font-family: "> </span><span style="font-size: 10pt; font-family: ">on</span><span style="font-size: 10pt; font-family: "> </span><span style="font-size: 10pt; font-family: ">the</span><span style="font-size: 10pt; font-family: "> </span><span style="font-size: 10pt; font-family: ">main</span><span style="font-size: 10pt; font-family: "> </span><span style="font-size: 10pt; font-family: ">page</span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: "><span style="font-size: 10pt; font-family: ">*</span><span style="font-size: 10pt; font-family: "> </span><span style="font-size: 10pt; font-family: ">This</span><span style="font-size: 10pt; font-family: "> </span><span style="font-size: 10pt; font-family: ">method</span><span style="font-size: 10pt; font-family: "> </span><span style="font-size: 10pt; font-family: ">also</span><span style="font-size: 10pt; font-family: "> </span><span style="font-size: 10pt; font-family: ">clears</span><span style="font-size: 10pt; font-family: "> </span><span style="font-size: 10pt; font-family: ">out</span><span style="font-size: 10pt; font-family: "> </span><span style="font-size: 10pt; font-family: ">the</span><span style="font-size: 10pt; font-family: "> </span><span style="font-size: 10pt; font-family: ">remaining</span><span style="font-size: 10pt; font-family: "> </span><span style="font-size: 10pt; font-family: ">selection</span><span style="font-size: 10pt; font-family: "> </span><span style="font-size: 10pt; font-family: ">of</span><span style="font-size: 10pt; font-family: "> </span><span style="font-size: 10pt; font-family: ">images.</span></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: "></span><span style="font-size: 10pt; font-family: ">*/</span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: "></span><strong><span style="font-size: 10pt; font-family: ">public</span></strong><span style="font-size: 10pt; font-family: "> </span><strong><span style="font-size: 10pt; font-family: ">void</span></strong><span style="font-size: 10pt; font-family: "> selectedImageRenderer(){</span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: "><span style="font-size: 10pt; font-family: ">String param = getRequest().getParameter(</span><span style="font-size: 10pt; font-family: ">&#8220;selectedImageId&#8221;</span><span style="font-size: 10pt; font-family: ">);</span></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: "></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: "></span><strong><span style="font-size: 10pt; font-family: ">for</span></strong><span style="font-size: 10pt; font-family: ">(</span><strong><span style="font-size: 10pt; font-family: ">int</span></strong><span style="font-size: 10pt; font-family: "> i=0;i&lt;</span><span style="font-size: 10pt; font-family: ">imageList</span><span style="font-size: 10pt; font-family: ">.size();i++){</span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: ">Object[] obj = (Object[])</span><span style="font-size: 10pt; font-family: ">imageList</span><span style="font-size: 10pt; font-family: ">.get(i);</span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: "></span><strong><span style="font-size: 10pt; font-family: ">if</span></strong><span style="font-size: 10pt; font-family: ">(obj[2].toString().equalsIgnoreCase(param)){</span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: ">obj[1] = </span><span style="font-size: 10pt; font-family: ">&#8220;selImage&#8221;</span><span style="font-size: 10pt; font-family: ">;</span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: ">}</span><strong><span style="font-size: 10pt; font-family: ">else</span></strong><span style="font-size: 10pt; font-family: ">{</span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: ">obj[1] = </span><span style="font-size: 10pt; font-family: ">&#8220;&#8221;</span><span style="font-size: 10pt; font-family: ">;</span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: ">}</span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: ">}</span></p>
<p><span style="font-size: 10pt; font-family: ">}</span></p>
<p><strong>Obj[0]</strong> contains Image Url<br />
<strong>Obj[1]</strong> contains selImage i.e. showing the selected image radio button checked.<br />
<strong>Obj[2]</strong> contains the ID of the selected image sent as a parameter from the page.</p>]]></content:encoded>
			<wfw:commentRss>http://www.zigron.com/blog/2008/06/18/image-selection-in-jsf/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
