Reloading and deleting from Vaadin ListBox



I am using a ListBox in Vaadin 14, populated by setItems with a list of custom objects. There two things I did not find out how to do:

  • Reload the listbox when the list of items hast changed
  • Deleting a certain object from the listbox

Answer

You can add/remove items from the underlying collection, and then call ListBox.getDataProvider().refreshAll().

For example,

ListBox<String> listBox = new ListBox<>();
List<String> items = new ArrayList<>();
items.add("one");
items.add("two");
listBox.setItems(items);

Button addItem = new Button("add item", e -> {
    items.add("three");
    listBox.getDataProvider().refreshAll();
});

add(listBox, addItem);


Source: stackoverflow