aboutsummaryrefslogtreecommitdiffstats
path: root/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxScrollToSelectedItem.java
blob: 174d136fd5891bbbfaee50f6c299f3153f640779 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.vaadin.tests.components.combobox;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.LongStream;

import com.vaadin.server.VaadinRequest;
import com.vaadin.tests.components.AbstractTestUI;
import com.vaadin.ui.ComboBox;

public class ComboBoxScrollToSelectedItem extends AbstractTestUI {

    @Override
    protected void setup(VaadinRequest request) {
        List<Item> items = new ArrayList<>();

        LongStream.range(1, 100)
                .forEach(l -> items.add(new Item(l, "item:" + l)));
        Item selectedItem = new Item(50l, "SHOW ME");
        items.set(50, selectedItem);

        ComboBox<Item> box = new ComboBox<>("items", items);
        box.setItemCaptionGenerator(Item::getName);
        box.setScrollToSelectedItem(true);
        box.setValue(selectedItem);

        addComponent(box);
    }

    public class Item {
        private Long id;
        private String name;

        public Item(long id, String name) {
            this.id = id;
            this.name = name;
        }

        public Long getId() {
            return id;
        }

        public void setId(Long id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }
}