Logo 
Search:

Java Answers

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds
  Question Asked By: Molly Torres   on Apr 12 In Java Category.

  
Question Answered By: Keith Marshall   on Apr 12

Try putting your data into a small wrapper object before adding it the the
combo. Just a simple class like:

class StringWrapper
{
private String val = null;

public StringWrapper(String val)
{
setString(val);
}

public void setString(String val)
{
this.val = val;
}

public String toString()
{
return this.val;
}
}

should do the trick.

Then do:

combo.addItem(new StringWrapper(stringValue));
...

String stringValue = null;
Object o = combo.getSelectedItem();
if(o != null)
{
stringValue = o.toString();
}

The other option would be to create your own ComboBoxModel and override the
setSelectedItem() function to use the "=" operator rather than the equals()
method when checking for equality. My sense is that this is probably the
best solution, but the most difficult to implement correctly (be sure to fire
all the correct events!).

Share: