You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ComboBoxScrollToSelectedItem.java 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package com.vaadin.tests.components.combobox;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import java.util.stream.LongStream;
  5. import com.vaadin.server.VaadinRequest;
  6. import com.vaadin.tests.components.AbstractTestUI;
  7. import com.vaadin.ui.ComboBox;
  8. public class ComboBoxScrollToSelectedItem extends AbstractTestUI {
  9. @Override
  10. protected void setup(VaadinRequest request) {
  11. List<Item> items = new ArrayList<>();
  12. LongStream.range(1, 100)
  13. .forEach(l -> items.add(new Item(l, "item:" + l)));
  14. Item selectedItem = new Item(50l, "SHOW ME");
  15. items.set(50, selectedItem);
  16. ComboBox<Item> box = new ComboBox<>("items", items);
  17. box.setItemCaptionGenerator(Item::getName);
  18. box.setScrollToSelectedItem(true);
  19. box.setValue(selectedItem);
  20. addComponent(box);
  21. }
  22. public class Item {
  23. private Long id;
  24. private String name;
  25. public Item(long id, String name) {
  26. this.id = id;
  27. this.name = name;
  28. }
  29. public Long getId() {
  30. return id;
  31. }
  32. public void setId(Long id) {
  33. this.id = id;
  34. }
  35. public String getName() {
  36. return name;
  37. }
  38. public void setName(String name) {
  39. this.name = name;
  40. }
  41. }
  42. }