use of com.vaadin.flow.component.listbox.ListBox in project flow-components by vaadin.
the class ListBoxDataViewPage method createIdentifierProviderForListBox.
private void createIdentifierProviderForListBox() {
CustomItem first = new CustomItem(1L, "First");
CustomItem second = new CustomItem(2L, "Second");
CustomItem third = new CustomItem(3L, "Third");
List<CustomItem> items = new ArrayList<>(Arrays.asList(first, second, third));
ListBox<CustomItem> listBox = new ListBox<>();
ListBoxListDataView<CustomItem> listDataView = listBox.setItems(items);
// Setting the following Identifier Provider makes the component
// independent from the CustomItem's equals method implementation:
listDataView.setIdentifierProvider(CustomItem::getId);
listBox.setValue(new CustomItem(3L));
Span selectedIdsSpan = new Span();
selectedIdsSpan.setId(LIST_BOX_SELECTED_IDS_SPAN);
selectedIdsSpan.setText(String.valueOf(listBox.getValue().getId()));
Button updateAndSelectByIdOnlyButton = new Button("Update & Select by Id", click -> {
// Make the names of unselected items similar to the name of
// selected one to mess with the <equals> implementation in
// CustomItem:
first.setName("Second");
listDataView.refreshItem(first);
third.setName("Second");
listDataView.refreshItem(third);
// Select the item not with the reference of existing item,
// and instead with just the Id:
listBox.setValue(new CustomItem(2L));
selectedIdsSpan.setText(String.valueOf(listBox.getValue().getId()));
});
updateAndSelectByIdOnlyButton.setId(LIST_BOX_SELECTION_BY_ID_UPDATE_BUTTON);
Button selectByIdAndNameButton = new Button("Select by Id and Name", click -> {
// Select the item with the Id and a challenging wrong name
// to verify <equals> method is not in use:
listBox.setValue(new CustomItem(3L, "Second"));
selectedIdsSpan.setText(String.valueOf(listBox.getValue().getId()));
});
selectByIdAndNameButton.setId(LIST_BOX_SELECTION_BY_ID_AND_NAME_UPDATE_BUTTON);
add(listBox, selectedIdsSpan, updateAndSelectByIdOnlyButton, selectByIdAndNameButton);
}
use of com.vaadin.flow.component.listbox.ListBox in project flow-components by vaadin.
the class ListBoxViewDemoPage method addDisabledListBox.
private void addDisabledListBox() {
Label message = new Label("-");
message.setId("message-label");
ListBox<String> listBox = new ListBox<>();
listBox.setItems("Bread", "Butter", "Milk");
listBox.setEnabled(false);
listBox.addValueChangeListener(event -> message.setText(String.format("Selection changed from %s to %s, selection is from client: %s", event.getOldValue(), event.getValue(), event.isFromClient())));
NativeButton button = new NativeButton("Select Milk", event -> listBox.setValue("Milk"));
Label note = new Label("Note! Even though updating from the client doesn't work, " + "the server may push a new status for the component.");
addCard("Disabled ListBox and selection", listBox, button, message, note).setId("disabled-list-box");
}
Aggregations