Search in sources :

Example 51 with ComboBox

use of com.vaadin.flow.component.combobox.ComboBox in project flow-components by vaadin.

the class ComboBoxDemoPage method createObjectComboBox.

private void createObjectComboBox() {
    Div message = createMessageDiv("object-selection-message");
    ComboBox<Song> comboBox = new ComboBox<>();
    comboBox.setLabel("Music selection");
    comboBox.setItemLabelGenerator(Song::getName);
    List<Song> listOfSongs = createListOfSongs();
    comboBox.setItems(listOfSongs);
    comboBox.addValueChangeListener(event -> {
        Song song = comboBox.getValue();
        if (song != null) {
            message.setText("Selected song: " + song.getName() + "\nFrom album: " + song.getAlbum() + "\nBy artist: " + song.getArtist());
        } else {
            message.setText("No song is selected");
        }
    });
    comboBox.getStyle().set(ElementConstants.STYLE_WIDTH, WIDTH_STRING);
    comboBox.setId("object-selection-box");
    add(new Div(new H2("Object selection"), comboBox, message));
}
Also used : Div(com.vaadin.flow.component.html.Div) ComboBox(com.vaadin.flow.component.combobox.ComboBox) H2(com.vaadin.flow.component.html.H2)

Example 52 with ComboBox

use of com.vaadin.flow.component.combobox.ComboBox in project flow-components by vaadin.

the class ComboBoxDemoPage method createComboBoxUsingComponentRenderer.

private void createComboBoxUsingComponentRenderer() {
    Div message = createMessageDiv("component-selection-message");
    ComboBox<Song> comboBox = new ComboBox<>();
    List<Song> listOfSongs = createListOfSongs();
    /*
         * Providing a custom item filter allows filtering based on all of the
         * rendered properties:
         */
    ItemFilter<Song> filter = (song, filterString) -> song.getName().toLowerCase().contains(filterString.toLowerCase()) || song.getArtist().toLowerCase().contains(filterString.toLowerCase());
    comboBox.setItems(filter, listOfSongs);
    comboBox.setItemLabelGenerator(Song::getName);
    comboBox.setRenderer(new ComponentRenderer<>(item -> {
        VerticalLayout container = new VerticalLayout();
        Label song = new Label(item.getName());
        container.add(song);
        Label artist = new Label(item.getArtist());
        artist.getStyle().set("fontSize", "smaller");
        container.add(artist);
        return container;
    }));
    comboBox.addValueChangeListener(event -> {
        if (event.getSource().isEmpty()) {
            message.setText("No artist selected");
        } else if (event.getOldValue() == null) {
            message.setText("Selected artist: " + event.getValue().getArtist());
        } else {
            message.setText("Selected artist: " + event.getValue().getArtist() + "\nThe old selection was: " + event.getOldValue().getArtist());
        }
    });
    comboBox.getStyle().set(ElementConstants.STYLE_WIDTH, WIDTH_STRING);
    comboBox.setId("component-selection-box");
    add(new Div(new H2("Using components"), new H2("Rendering items using ComponentTemplateRenderer"), comboBox, message));
}
Also used : Div(com.vaadin.flow.component.html.Div) IntStream(java.util.stream.IntStream) ItemFilter(com.vaadin.flow.component.combobox.ComboBox.ItemFilter) ComponentRenderer(com.vaadin.flow.data.renderer.ComponentRenderer) VerticalLayout(com.vaadin.flow.component.orderedlayout.VerticalLayout) Div(com.vaadin.flow.component.html.Div) Label(com.vaadin.flow.component.html.Label) H2(com.vaadin.flow.component.html.H2) ComboBox(com.vaadin.flow.component.combobox.ComboBox) Collectors(java.util.stream.Collectors) Route(com.vaadin.flow.router.Route) ArrayList(java.util.ArrayList) List(java.util.List) Faker(com.github.javafaker.Faker) TemplateRenderer(com.vaadin.flow.data.renderer.TemplateRenderer) ElementConstants(com.vaadin.flow.dom.ElementConstants) ComboBox(com.vaadin.flow.component.combobox.ComboBox) Label(com.vaadin.flow.component.html.Label) VerticalLayout(com.vaadin.flow.component.orderedlayout.VerticalLayout) H2(com.vaadin.flow.component.html.H2)

Example 53 with ComboBox

use of com.vaadin.flow.component.combobox.ComboBox in project flow-components by vaadin.

the class ComboBoxPage method createWithRequestsSpyDataProvider.

private void createWithRequestsSpyDataProvider() {
    ComboBox<String> comboBox = new ComboBox<>();
    comboBox.setId("combobox-list-size-request-count");
    AtomicInteger sizeRequestCounter = new AtomicInteger(0);
    Span sizeRequestCountSpan = new Span("0");
    sizeRequestCountSpan.setId("list-size-request-count-span");
    ListDataProvider<String> dataProvider = new SpyListDataProvider<>(Arrays.asList("foo", "bar", "baz"), ignore -> sizeRequestCountSpan.setText(String.valueOf(sizeRequestCounter.incrementAndGet())));
    comboBox.setDataProvider(dataProvider);
    NativeButton resetDataProvider = new NativeButton("Set data provider", event -> comboBox.setItems("new item"));
    resetDataProvider.setId("size-request-count-update-provider");
    add(comboBox, resetDataProvider, sizeRequestCountSpan);
}
Also used : NativeButton(com.vaadin.flow.component.html.NativeButton) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ComboBox(com.vaadin.flow.component.combobox.ComboBox) Span(com.vaadin.flow.component.html.Span)

Example 54 with ComboBox

use of com.vaadin.flow.component.combobox.ComboBox in project flow-components by vaadin.

the class ComboBoxPage method createExternalSetValue.

private void createExternalSetValue() {
    ComboBox<String> comboBox = new ComboBox<>();
    comboBox.setItems("foo", "bar");
    comboBox.setId("external-selected-item");
    comboBox.setValue("foo");
    NativeButton changeSelectedItem = new NativeButton("Changed selected item", evt -> comboBox.setValue("bar".equals(comboBox.getValue()) ? "foo" : "bar"));
    changeSelectedItem.setId("toggle-selected-item");
    add(comboBox, changeSelectedItem);
}
Also used : NativeButton(com.vaadin.flow.component.html.NativeButton) ComboBox(com.vaadin.flow.component.combobox.ComboBox)

Example 55 with ComboBox

use of com.vaadin.flow.component.combobox.ComboBox in project flow-components by vaadin.

the class ComboBoxPage method createExternalDisableTest.

private void createExternalDisableTest() {
    ComboBox<String> comboBox = new ComboBox<>();
    comboBox.setId("client-test");
    Label message = new Label("Nothing clicked yet...");
    message.setId("get-value");
    comboBox.setItems("foo", "bar", "paa");
    NativeButton valueSet = new NativeButton("Set Value");
    valueSet.setId("set-value");
    valueSet.addClickListener(event -> {
        comboBox.setValue("bar");
        message.setText(comboBox.getValue());
    });
    NativeButton disableCB = new NativeButton("Set Disabled");
    disableCB.setId("disable-combo-box");
    disableCB.addClickListener(event -> {
        comboBox.setEnabled(false);
        message.setText(comboBox.getValue());
    });
    add(comboBox, valueSet, disableCB, message);
}
Also used : NativeButton(com.vaadin.flow.component.html.NativeButton) ComboBox(com.vaadin.flow.component.combobox.ComboBox) Label(com.vaadin.flow.component.html.Label)

Aggregations

ComboBox (com.vaadin.flow.component.combobox.ComboBox)69 Div (com.vaadin.flow.component.html.Div)35 Span (com.vaadin.flow.component.html.Span)21 NativeButton (com.vaadin.flow.component.html.NativeButton)20 Route (com.vaadin.flow.router.Route)18 List (java.util.List)16 H2 (com.vaadin.flow.component.html.H2)14 Collectors (java.util.stream.Collectors)14 Label (com.vaadin.flow.component.html.Label)12 ComponentRenderer (com.vaadin.flow.data.renderer.ComponentRenderer)12 Stream (java.util.stream.Stream)12 Component (com.vaadin.flow.component.Component)10 VerticalLayout (com.vaadin.flow.component.orderedlayout.VerticalLayout)10 ArrayList (java.util.ArrayList)10 Button (com.vaadin.flow.component.button.Button)9 Person (com.vaadin.flow.component.combobox.test.entity.Person)9 PersonService (com.vaadin.flow.component.combobox.test.service.PersonService)9 IntStream (java.util.stream.IntStream)9 ItemFilter (com.vaadin.flow.component.combobox.ComboBox.ItemFilter)8 Paragraph (com.vaadin.flow.component.html.Paragraph)8