Loading...
Important Notice - Forums is archived

To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Product icon
TUTORIAL

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.

V8 ComboBox value set

Nicklas Karlsson
5 years ago Mar 21, 2017 10:38pm
Alejandro C De Baca
5 years ago Mar 22, 2017 3:44am

It would be helpful if you shared some code, particularly the classes that you are using as the generic types for A and B.

In any case, I've worked up a simple example that shows how to use V8 ComboBox with a basic String as well as a custom class. In short, if you have a custom class for B, you should create an instance that returns the empty string from its toString() method, or whatever method you may have passed to .setItemCaptionGenerator() on ComboxBox B. If you are using String as the generic type for B, just pass the empty string.

public class MyUI extends UI {
    private static class StringWrapper {
        private final String string;
        public StringWrapper(String string) { this.string = string; }
        @Override public String toString() { return string; }
        public static final StringWrapper EMPTY = new StringWrapper("");
    }

    private final HashMap<String, List<String>> data1 =
        new HashMap<String, List<String>>() {{
            put("A", Arrays.asList("A1", "A2", "A3"));
            put("B", Arrays.asList("B1", "B2", "B3"));
            put("C", Arrays.asList("C1", "C2", "C3"));
            put("D", Arrays.asList());
        }};

    private final HashMap<String, List<StringWrapper>> data2 =
        new HashMap<String, List<StringWrapper>>() {{
            put("A", Arrays.asList(new StringWrapper("A1"), new StringWrapper("A2"), new StringWrapper("A3")));
            put("B", Arrays.asList(new StringWrapper("B1"), new StringWrapper("B2"), new StringWrapper("B3")));
            put("C", Arrays.asList(new StringWrapper("C1"), new StringWrapper("C2"), new StringWrapper("C3")));
            put("D", Arrays.asList());
        }};

    @Override protected void init(VaadinRequest vaadinRequest) {
        final HorizontalLayout boxLayout = new HorizontalLayout();
        setContent(boxLayout);

        final ComboBox<String> box1 = new ComboBox<String>();
        final ComboBox<String> box2 = new ComboBox<String>();
        final ComboBox<StringWrapper> box3 = new ComboBox<StringWrapper>();
        boxLayout.addComponents(box1, box2, box3);

        box1.addValueChangeListener(event -> {
            // Swap out values for box2 based on what's selected in box1
            List<String> items = data1.get(box1.getValue());
            box2.setItems(items);
            box2.setSelectedItem(items.stream().findFirst().orElse(""));
        });

        box1.addValueChangeListener(event -> {
            /*
             * Swap out values for box3 based on what's selected in box1. Here we
             * use a custom class and a static instance whose toString() returns
             * the empty string.
             */
            List<StringWrapper> items = data2.get(box1.getValue());
            box3.setItems(items);
            box3.setSelectedItem(items.stream().findFirst().orElse(StringWrapper.EMPTY));
        });

        Set<String> items = data1.keySet();
        box1.setItems(items);
        box1.setSelectedItem(items.stream().findFirst().get());
        box1.setEmptySelectionAllowed(false);
    }

    @WebServlet(urlPatterns = "/*", name = "MyUIServlet", asyncSupported = true)
    @VaadinServletConfiguration(ui = MyUI.class, productionMode = false)
    public static class MyUIServlet extends VaadinServlet {}
}
Nicklas Karlsson
5 years ago Mar 23, 2017 5:35am
Alejandro C De Baca
5 years ago Mar 23, 2017 3:14pm
Nicklas Karlsson
5 years ago Mar 23, 2017 4:28pm
Nicklas Karlsson
5 years ago Mar 25, 2017 4:06pm