How to select a ComboBox item

Hi All,

I have created a ComboBox and I put inside a list of objects.
Each object has an ID and a DESCRIPTION.

public class ArticleBean implements Serializable {

private int id;
private String description;

}

I can display the ComboBox but I can’t select a particular item of it.
I mean, I tried to do this : comboBox.select(new ArticleBean(1,“apple”)) but it doesn’t work.
What’s the trick ?
Cheers.

                      Stefano

Hey,

After you’ve created your ComboBox, and added items to it, you can select an item via its id.

ComboBox cb = new ComboBox();
cb.addItem("Lorem");
cb.addItem("Ipsum");

cb.select("Ipsum");

So if you do:

ArticleBean ab = new ArticleBean(1,"apple");
comboBox.addItem(ab);

Then you should be able to select it via:

comboBox.select(ab);

Hi Joacim,

tahnks for your reply, but my case something like this :

ArticleBean ab1 = new ArticleBean(1,“apple1”);
ArticleBean ab2 = new ArticleBean(2,“apple2”);
ArticleBean ab3 = new ArticleBean(3,“apple3”);
comboBox.addItem(ab1);
comboBox.addItem(ab2);
comboBox.addItem(ab3);

ArticleBean abToSelect = new ArticleBean(3,“apple3”)

comboBox.select(abToSelect);

This doesn’t work.

I am thinking to extract the collection of items from the ComboBox and do some searching inside it.
What do you think ?
Cheers.

                  Stefano

Hey Stefano,

That won’t work as you are creating a new ArticleBean which hasn’t been added to the ComboBox. You would have to use the object that was added.

Hi Joacim,

ok.
Thanks.
Cheers.

            Stefano

It would work if the
ArticleBean
had implemented
equals
and
hashCode
methods.

Hi Agata,

I’ll try :slight_smile:
Cheers.

           Stefano

Touché! :slight_smile: