Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

ComboBox Again

  Asked By: Molly    Date: Apr 12    Category: Java    Views: 662

I already asked this question but I'll ask again because I didn't solved my
problem yet :o(

How do I treat repeated items from a combo box?

I only could manage the selected indexes in MutableComboBoxModel or
ComboBoxModel by its Text, so if the combo has two repeated values I not able to
know if the selected item is the first or the second...

Any idea about how to fix it???



1 Answer Found

Answer #1    Answered By: Keith Marshall     Answered 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)

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!).

Didn't find what you were looking for? Find more on ComboBox Again Or get search suggestion and latest updates.