I want a JComboBox that shows names (e.g. Mary, Fred, Alice, Joe) but
whose getSelectedItem() returns a code (e.g. 21, 17, 3, 11).
Should I ...
Plan A: Implement a ListCellRenderer for `new JComboBox(codeArray)`
which shows the name not the code.
or
Plan B: Extend JComboBox so that I can invoke it `JComboBox(codeArray,
nameArray)` - or something similar, maybe pass it a TreeMap or something?
or
Plan C: use `new JComboBox(nameArray)` and write a method that converts
names to codes and use this in my JComboBox's ActionListener?
or
Something completely different?
Thomas Weidenfeller - 23 Aug 2006 14:52 GMT
> Plan C: use `new JComboBox(nameArray)` and write a method that converts names to codes and use this in my JComboBox's ActionListener?
>
> or
> Something completely different?
C, but with simply using a HashMap, or e.g.:
public class ComboBoxItem {
protected String name;
protected int value;
public ComboBoxItem(String name, int value) {
this.name = name;
this.value = value;
}
public int getValue() { return value; }
public String toString() { return name; }
}
Object items[] = {
new ComboBoxItem("Mary", 21),
new ComboBoxItem("Fred", 17),
...
};
JComboBox cb = new JComboBox(items);
...
Object o = cb.getSelectedItem();
if(o instanceof ComboBoxItem) { // implicit check for null
int value = ((ComboBoxItem)o).getValue();
}
/Thomas

Signature
The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/
Ian Wilson - 23 Aug 2006 16:21 GMT
>> Plan C: use `new JComboBox(nameArray)` and write a method that
>> converts names to codes and use this in my JComboBox's ActionListener?
[quoted text clipped - 34 lines]
> int value = ((ComboBoxItem)o).getValue();
> }
Thanks Thomas. That is very clear and elegant.
bkk.ngroup@gmail.com - 23 Aug 2006 15:18 GMT
> I want a JComboBox that shows names (e.g. Mary, Fred, Alice, Joe) but
> whose getSelectedItem() returns a code (e.g. 21, 17, 3, 11).
Are you certain you want the getSelectedItem() method to directly
return the code ? Would it be OK if
((SomeObject)getSelectedItem()).getCode() gave you the code ?
If that's acceptable, the solution is simple. Create a class with these
two properties - say "name" and "code" (I'd suppose you probably have a
class already whose properties these are - in which case you don't need
to create one afresh), Override the toString() method to return the
name, Now instead of passing an array of Strings to the JComboBox
constructior, pass an Array of the instances of this class. The default
renderer will call toString() on these objects to display the text -
while getSelectedItem() will return the selected object. (You can now
access the code property after casting it to the correct class)
BK
Ian Wilson - 23 Aug 2006 16:25 GMT
>> I want a JComboBox that shows names (e.g. Mary, Fred, Alice, Joe)
>> but whose getSelectedItem() returns a code (e.g. 21, 17, 3, 11).
>
> Are you certain you want the getSelectedItem() method to directly
> return the code ? Would it be OK if
> ((SomeObject)getSelectedItem()).getCode() gave you the code ?
You are right, In my specific circumstances it isn't essential for the
getSelectedItem() to give the code instead of the name.
> If that's acceptable, the solution is simple. Create a class with
> these two properties - say "name" and "code" (I'd suppose you
[quoted text clipped - 6 lines]
> object. (You can now access the code property after casting it to the
> correct class)
Thanks BK, your explanation nicely complements Thomas' example.