Urgent! JComboBox.setSelectedItem(x) problem
I have a couple of JComboBoxes that I have popluated with custom objects. There is a known universe of these custom objects and they are all contained within the JComboBox.
When I want to set the item to be displayed in the combo, I have a .getSelectedItem(x), where x is the same type of custom object as the combo is populated with.The problem is that it never appears as selected, just the first one in the combo is displayed.
This is really driving me nuts! I have used this same type of code in several places for displaying custom objects within a combo and I have never had this problem before. The only thing I can think of that is different is that there is 125 of these objects within the combo and they all display fine, but I don't know if for some reason there is a problem with setSelectedItem(x) when there are so many objects in the combo (the other times I have done this and it has worked there has never been more than 40 or so).
If someone could help, I would greatly appreciate it!!
(I am using 1.3, but have also tried 1.2.2)
Thanks
[1099 byte] By [
whysman] at [2007-9-26 2:03:27]

you're using custom items, did you also customized the equals( Object ) method as well? Are you using the default combobox model or did you write your own?
remu at 2007-6-29 8:46:14 >

> you're using custom items, did you also customized the
> equals( Object ) method as well? Are you using
> the default combobox model or did you write your own?
The equals & hashcode methods have been taken care of, but I am using the default combobox model.
I just make a simple code, I was filling my JComboBox with strings, then I tried to set the selected item to new Integer(10), which can not be equals to any of the item of the combobox, and so the combobox did not displayed my Integer. If your custom items do not override the equals method and rely on the default Object implementation, then 2 different instances of your CustomItem class will never be equal, and so, your JComboBox will never be able to select them.
solution if it was the problem:
- either always use the same instance.
- override the equals(Object) method if possible.
remu at 2007-6-29 8:46:14 >

> The equals & hashcode methods have been taken care of,
> but I am using the default combobox model.
hum hum, ok. hashcode is not used by JComboBox nor DefaultComboBoxModel, so I will only need to see your code for:
- ComboBox/ComboBoxModel creation
- CustomItem equals method
- the portion of code related to setSelectedIndex( item ), with the item creation process if applicable.
remu at 2007-6-29 8:46:14 >

> > The equals & hashcode methods have been taken care
> of,
> > but I am using the default combobox model.
>
> hum hum, ok. hashcode is not used by JComboBox nor
> DefaultComboBoxModel, so I will only need to see your
> code for:
> - ComboBox/ComboBoxModel creation
> - CustomItem equals method
> - the portion of code related to setSelectedIndex(
> item ), with the item creation process if applicable.
>
I believe hashcode is used during .equals() though....
anyway, here it is..
<b>Custom object equals method....</b>
public boolean equals (Object target) {
if (target == null)
return false;
if (!(target instanceof CasaIndustry))
return false;
CasaIndustry ci = (CasaIndustry) target;
if (this == ci)
return true;
return false;
}
<b> combo box creation and seeding..... </b>
sandpClassificationJComboBox = new JComboBox((UIHelper.getInstance().getSandPIndustries()).toArray());
(NOTE this returns back an arraylist of these custom objects, then I use .toArray on the arraylist to seed the combo)
<b> trying to set the index.... </b>
IssuerIndustry sandpII = UI.getCasaClient().loadIndustry(suii, TaxonomyID.valueOf(3));
CasaIndustry s = CasaRootIndustry.StandardPoors.find(sandpII.getTaxonomyID());
sandpClassificationJComboBox.setSelectedItem(s);
(NOTE: the custom object in question is the CasaIndustry)
..ooopps I used the wrong tags above to emphasize my headings... oh well, you get the idea..
> I believe hashcode is used during .equals()
> though....
that depends on your equals implementation, yours do not use that hascode, so ... no, it's definitely not used :) hashcode is used to speed up object read/write process in certain Map implementations such as HashMap and Hashtable.
> public boolean equals (Object target) {
> if (target == null)
> return false;
>
> if (!(target instanceof CasaIndustry))
> return false;
>
> CasaIndustry ci = (CasaIndustry) target;
>
> if (this == ci)
> return true;
>
> return false;
> }
well, "null instanceof Whatever" always return false, see java spec for it. And by the way, your equals method would run far more faster if you just do this:
return this == target
which is by the way, the default Object implementation, so if you class just extends Object, you should not even bother overridding it :)
> JComboBox((UIHelper.getInstance().getSandPIndustries())
> toArray());
CasaRootIndustry.StandardPoors.find(sandpII.getTaxonomy
> D());
since your equals code just test for instance equality, are you sure sandpII.getTaxonomyID() returns an instance of CasaIndustry that did exist in the list returned by UIHelper.getInstance().getSandPIndustries(). The getInstance lets suppose otherwise.
remu at 2007-6-29 8:46:14 >

> since your equals code just test for instance
> equality, are you sure sandpII.getTaxonomyID()
> returns an instance of CasaIndustry that did
> exist in the list returned by
> UIHelper.getInstance().getSandPIndustries().
> The getInstance lets suppose otherwise.
sandpII.getTaxonomyID() returns an ID so that I can get an instance of CasaIndustry. This is structured correctly, the getInstance call is the access point into the singleton class UIHelper and is not indicative of the return type of my custom object.UIHelper.getInstance().getSandPIndustries() returns an arraylist of CasaIndustry's (see initial problem explaination).
After my last post, I took out the instanceof check in my equals method and reworked it slightly. still no luck.
Ok, I start to run out of explanations, My last check would be to add a System.out.println, just to be sure the find() returned a valid CasaIndustry object and not null, right before trying to select the item.
remu at 2007-6-29 8:46:14 >

I had to rewrite my equals method to seperately check a few instance fields. Now it works. Thanks.