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));
}
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));
}
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);
}
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);
}
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);
}
Aggregations