3

I'm populating a ListBox with a list of entities from a MySQL source. I can't see how to tell it which field to use as its ID, and which to use as its display.

For ComboBox I can use setItemLabelGenerator to tell it which field to use for display, but I can't see what the equivalent would be for ListBox.

ListBox<MyEntitySource> entityListBox = new ListBox<MyEntitySource>();
List<MyEntitySource> entitySource = (List<MyEntitySource>) entityRepository.findAll();
entityListBox.setItems(entitySource);

This ends up displaying as eg: MyEntity{id=1, description=Item 1}

How do I tell it to hold the ID as field "id" and display the value as field "description"?

Anna Koskinen
  • 1,362
  • 3
  • 22
the Ben B
  • 166
  • 8
  • For the entity you don't need to handle this, the ListBox will return your POJO item, and then you can use all of your mehtods. Also setting the selected item works via the item himself – André Schild Oct 20 '21 at 07:34

1 Answers1

4

You need to apply a renderer for the items, something like:

entityListBox.setRenderer(new TextRenderer<>(entity -> entity.getDescription()));
Anna Koskinen
  • 1,362
  • 3
  • 22
Tatu Lund
  • 9,949
  • 1
  • 12
  • 26