<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Not a JComboBox Bug</title>
	<atom:link href="http://stuffthathappens.com/blog/2009/09/07/not-a-jcombobox-bug/feed/" rel="self" type="application/rss+xml" />
	<link>http://stuffthathappens.com/blog/2009/09/07/not-a-jcombobox-bug/</link>
	<description>Technology and Geek Stuff by Eric Burke</description>
	<lastBuildDate>Thu, 04 Nov 2010 22:37:00 -0400</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Avrom</title>
		<link>http://stuffthathappens.com/blog/2009/09/07/not-a-jcombobox-bug/comment-page-1/#comment-47811</link>
		<dc:creator>Avrom</dc:creator>
		<pubDate>Mon, 14 Sep 2009 13:31:50 +0000</pubDate>
		<guid isPermaLink="false">http://stuffthathappens.com/blog/?p=1352#comment-47811</guid>
		<description>Like Danno Ferrin wrote, I too work on an application that has a &quot;public name&quot; and a &quot;private name&quot; that is a unique key for items in a combo box. Unfortunately, the original programmer who implemented our subclass of JComboBox populated it with Strings so we had the unfortunate incident of where a user would select the second instance of a duplicate entry and have it flip to the first selection after.
Since fixing this bug would require that I rewrite a whole bunch of code from scratch, I instead opted to leave it alone and instead put in a validation for when users try to create duplicate entries in the first place.</description>
		<content:encoded><![CDATA[<p>Like Danno Ferrin wrote, I too work on an application that has a &#8220;public name&#8221; and a &#8220;private name&#8221; that is a unique key for items in a combo box. Unfortunately, the original programmer who implemented our subclass of JComboBox populated it with Strings so we had the unfortunate incident of where a user would select the second instance of a duplicate entry and have it flip to the first selection after.<br />
Since fixing this bug would require that I rewrite a whole bunch of code from scratch, I instead opted to leave it alone and instead put in a validation for when users try to create duplicate entries in the first place.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Eric Burke</title>
		<link>http://stuffthathappens.com/blog/2009/09/07/not-a-jcombobox-bug/comment-page-1/#comment-47285</link>
		<dc:creator>Eric Burke</dc:creator>
		<pubDate>Tue, 08 Sep 2009 17:01:12 +0000</pubDate>
		<guid isPermaLink="false">http://stuffthathappens.com/blog/?p=1352#comment-47285</guid>
		<description>@ivucica the getSelectedIndex() method does not &quot;pretend that several entries are the same because they have the same caption&quot;, despite what the person said on the bug database. 

The displayed value is determined by a renderer. Thus, your renderer might display duplicates, but it&#039;s up to you to ensure each item in your data model is unique. Typically this is done via a unique identifier from a database table.

The getSelectedIndex() method does not operate based on the renderer. The JavaDoc comment is very clear: &quot;Returns the first item in the list that matches the given item.&quot;. And the implementation loops through the data model, and calls .equals() on each object in the model.

If a programmer puts two objects into a data model where both are .equals() to each other, that&#039;s really not a combo box bug.</description>
		<content:encoded><![CDATA[<p>@ivucica the getSelectedIndex() method does not &#8220;pretend that several entries are the same because they have the same caption&#8221;, despite what the person said on the bug database. </p>
<p>The displayed value is determined by a renderer. Thus, your renderer might display duplicates, but it&#8217;s up to you to ensure each item in your data model is unique. Typically this is done via a unique identifier from a database table.</p>
<p>The getSelectedIndex() method does not operate based on the renderer. The JavaDoc comment is very clear: &#8220;Returns the first item in the list that matches the given item.&#8221;. And the implementation loops through the data model, and calls .equals() on each object in the model.</p>
<p>If a programmer puts two objects into a data model where both are .equals() to each other, that&#8217;s really not a combo box bug.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ivucica</title>
		<link>http://stuffthathappens.com/blog/2009/09/07/not-a-jcombobox-bug/comment-page-1/#comment-47283</link>
		<dc:creator>ivucica</dc:creator>
		<pubDate>Tue, 08 Sep 2009 16:50:53 +0000</pubDate>
		<guid isPermaLink="false">http://stuffthathappens.com/blog/?p=1352#comment-47283</guid>
		<description>You should have read one other comment on that linked bug; while duplicates may and do confuse users, they may be invaluable for debugging. If the function is supposed to get selected index, it&#039;s supposed to do just that, not pretend that several entries are the same because they have the same caption. A single GUI element is not a single database entry -- it may not even be related to a database.

Overall, from user usability standpoint, I agree with your post. However the bug is still in the combobox implementation: it should avoid _adding_ duplicates into the list instead of just behaving in a dumb way. Even Apple&#039;s solution is not good.</description>
		<content:encoded><![CDATA[<p>You should have read one other comment on that linked bug; while duplicates may and do confuse users, they may be invaluable for debugging. If the function is supposed to get selected index, it&#8217;s supposed to do just that, not pretend that several entries are the same because they have the same caption. A single GUI element is not a single database entry &#8212; it may not even be related to a database.</p>
<p>Overall, from user usability standpoint, I agree with your post. However the bug is still in the combobox implementation: it should avoid _adding_ duplicates into the list instead of just behaving in a dumb way. Even Apple&#8217;s solution is not good.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Danno Ferrin</title>
		<link>http://stuffthathappens.com/blog/2009/09/07/not-a-jcombobox-bug/comment-page-1/#comment-47272</link>
		<dc:creator>Danno Ferrin</dc:creator>
		<pubDate>Tue, 08 Sep 2009 15:22:22 +0000</pubDate>
		<guid isPermaLink="false">http://stuffthathappens.com/blog/?p=1352#comment-47272</guid>
		<description>At my day job we tend to have combo box lists with &#039;public names&#039; that are pretty and &#039;private names&#039; that are keys, or uuids, row numbers, or some other transformation, so this situation happens on occasion.  (we don&#039;t actually use public name and private name as our lingo, that&#039;s just the lingo I got from my first job where I was working on Data Warehouse software).

What we have done to fix is is we don&#039;t populate the ComboBox model with strings, but with a class that holds the public and private name with equals and hashCode running entirely off of the private name.  We then slap a renderer on the combo box that will render the public name, so the ComboBox runs off of a discreet class without many conflicts.

Lucky for us our customers are a bit more reasonable, when presented with a public name collision we&#039;ve always been able to get them to make the public names unique.  Probably because they already have an appreciation of the problem when the split db keys away from user presentable data.</description>
		<content:encoded><![CDATA[<p>At my day job we tend to have combo box lists with &#8216;public names&#8217; that are pretty and &#8216;private names&#8217; that are keys, or uuids, row numbers, or some other transformation, so this situation happens on occasion.  (we don&#8217;t actually use public name and private name as our lingo, that&#8217;s just the lingo I got from my first job where I was working on Data Warehouse software).</p>
<p>What we have done to fix is is we don&#8217;t populate the ComboBox model with strings, but with a class that holds the public and private name with equals and hashCode running entirely off of the private name.  We then slap a renderer on the combo box that will render the public name, so the ComboBox runs off of a discreet class without many conflicts.</p>
<p>Lucky for us our customers are a bit more reasonable, when presented with a public name collision we&#8217;ve always been able to get them to make the public names unique.  Probably because they already have an appreciation of the problem when the split db keys away from user presentable data.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Logan</title>
		<link>http://stuffthathappens.com/blog/2009/09/07/not-a-jcombobox-bug/comment-page-1/#comment-47266</link>
		<dc:creator>Logan</dc:creator>
		<pubDate>Tue, 08 Sep 2009 12:38:45 +0000</pubDate>
		<guid isPermaLink="false">http://stuffthathappens.com/blog/?p=1352#comment-47266</guid>
		<description>I&#039;ve had the same request from clients regarding websites.  In particular, one client had an application that included a (large) filterable employee list, but it only showed the user the employees&#039; names.  Oops.

BTW, using indentation to denote option categories in a combo box has more problems than just this one.  What happens if I select a category like &quot;platter&quot;, instead of an option?</description>
		<content:encoded><![CDATA[<p>I&#8217;ve had the same request from clients regarding websites.  In particular, one client had an application that included a (large) filterable employee list, but it only showed the user the employees&#8217; names.  Oops.</p>
<p>BTW, using indentation to denote option categories in a combo box has more problems than just this one.  What happens if I select a category like &#8220;platter&#8221;, instead of an option?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tbee</title>
		<link>http://stuffthathappens.com/blog/2009/09/07/not-a-jcombobox-bug/comment-page-1/#comment-47263</link>
		<dc:creator>Tbee</dc:creator>
		<pubDate>Tue, 08 Sep 2009 12:20:33 +0000</pubDate>
		<guid isPermaLink="false">http://stuffthathappens.com/blog/?p=1352#comment-47263</guid>
		<description>Even though duplicate values are not good, it requires only a small change to allow JComboBoxes to use Maps; show the description, use the key. 

Just extend JCombobox, have a constructor that takes a map and from that extract the keyset as the actual values for the combobox. Then define a renderer that shows the value from the map.

	public JComboBox(final Map items)
	{
		super(new Vector( items.keySet() ));
		
		// set the renderer
		setRenderer(new ListCellRenderer()
		{
			public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)
			{
				// find the key for this value
				if (!items.containsKey(value)) throw new IllegalArgumentException(&quot;The map does not contain key: &quot; + value);
				iJLabel.setText( items.get(value).toString()  );
				return iJLabel;
			}
			JLabel iJLabel = new JLabel(&quot;&quot;);
		});
	}</description>
		<content:encoded><![CDATA[<p>Even though duplicate values are not good, it requires only a small change to allow JComboBoxes to use Maps; show the description, use the key. </p>
<p>Just extend JCombobox, have a constructor that takes a map and from that extract the keyset as the actual values for the combobox. Then define a renderer that shows the value from the map.</p>
<p>	public JComboBox(final Map items)<br />
	{<br />
		super(new Vector( items.keySet() ));</p>
<p>		// set the renderer<br />
		setRenderer(new ListCellRenderer()<br />
		{<br />
			public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)<br />
			{<br />
				// find the key for this value<br />
				if (!items.containsKey(value)) throw new IllegalArgumentException(&#8221;The map does not contain key: &#8221; + value);<br />
				iJLabel.setText( items.get(value).toString()  );<br />
				return iJLabel;<br />
			}<br />
			JLabel iJLabel = new JLabel(&#8221;");<br />
		});<br />
	}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: yd</title>
		<link>http://stuffthathappens.com/blog/2009/09/07/not-a-jcombobox-bug/comment-page-1/#comment-47261</link>
		<dc:creator>yd</dc:creator>
		<pubDate>Tue, 08 Sep 2009 12:13:48 +0000</pubDate>
		<guid isPermaLink="false">http://stuffthathappens.com/blog/?p=1352#comment-47261</guid>
		<description>Formatting was lost above - imagine indentation for the Turkey/Fish entries.

I also don&#039;t have any problem with blocking the duplicates as it might force people to go for the better options.</description>
		<content:encoded><![CDATA[<p>Formatting was lost above &#8211; imagine indentation for the Turkey/Fish entries.</p>
<p>I also don&#8217;t have any problem with blocking the duplicates as it might force people to go for the better options.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: yd</title>
		<link>http://stuffthathappens.com/blog/2009/09/07/not-a-jcombobox-bug/comment-page-1/#comment-47260</link>
		<dc:creator>yd</dc:creator>
		<pubDate>Tue, 08 Sep 2009 12:12:41 +0000</pubDate>
		<guid isPermaLink="false">http://stuffthathappens.com/blog/?p=1352#comment-47260</guid>
		<description>Sadly, I&#039;ve had duplicate strings before, and used the index to differentiate (in VB6, I imagine, but maybe HTML).

For one example, imagine a hierarchy of items where there are sub-elements with the same title:

Sandwich
   Turkey
   Fish
Platter
   Turkey
   Fish

Sure there are better controls for this, but sometimes due to other design restraints this makes some sense.  I can&#039;t think of an example, but I&#039;ve had cases where all items (parents and children) can be selected, so in the example above you might choose &quot;Sandwich&quot; or &quot;Turkey&quot; (under sandwich).

Yes, there are other ways to do this (and I would try my best to find a better way), but this is a semi-reasonable use for same values in the list.</description>
		<content:encoded><![CDATA[<p>Sadly, I&#8217;ve had duplicate strings before, and used the index to differentiate (in VB6, I imagine, but maybe HTML).</p>
<p>For one example, imagine a hierarchy of items where there are sub-elements with the same title:</p>
<p>Sandwich<br />
   Turkey<br />
   Fish<br />
Platter<br />
   Turkey<br />
   Fish</p>
<p>Sure there are better controls for this, but sometimes due to other design restraints this makes some sense.  I can&#8217;t think of an example, but I&#8217;ve had cases where all items (parents and children) can be selected, so in the example above you might choose &#8220;Sandwich&#8221; or &#8220;Turkey&#8221; (under sandwich).</p>
<p>Yes, there are other ways to do this (and I would try my best to find a better way), but this is a semi-reasonable use for same values in the list.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sakuraba</title>
		<link>http://stuffthathappens.com/blog/2009/09/07/not-a-jcombobox-bug/comment-page-1/#comment-47235</link>
		<dc:creator>Sakuraba</dc:creator>
		<pubDate>Tue, 08 Sep 2009 06:09:20 +0000</pubDate>
		<guid isPermaLink="false">http://stuffthathappens.com/blog/?p=1352#comment-47235</guid>
		<description>I think the key/value approach of html comboboxes is better, because they give you the choice to have both - duplicates allowed or not.</description>
		<content:encoded><![CDATA[<p>I think the key/value approach of html comboboxes is better, because they give you the choice to have both &#8211; duplicates allowed or not.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.264 seconds -->

