use of com.vaadin.flow.component.checkbox.Checkbox in project flow-components by vaadin.
the class FormLayoutView method createCompositeLayout.
private void createCompositeLayout() {
// And then just use it like a regular component
MyCustomLayout layout = new MyCustomLayout();
TextField name = new TextField();
TextField email = new TextField();
Checkbox emailUpdates = new Checkbox("E-mail me updates");
layout.addItemWithLabel("Name", name);
// Both the email field and the emailUpdates checkbox are wrapped inside
// the same form item
layout.addItemWithLabel("E-mail", email, emailUpdates);
addCard("Using form layout inside a composite", layout);
}
use of com.vaadin.flow.component.checkbox.Checkbox in project flow-components by vaadin.
the class FormLayoutView method createFormLayoutWithBinder.
private void createFormLayoutWithBinder() {
FormLayout layoutWithBinder = new FormLayout();
Binder<Contact> binder = new Binder<>();
// The object that will be edited
Contact contactBeingEdited = new Contact();
// Create the fields
TextField firstName = new TextField();
firstName.setValueChangeMode(ValueChangeMode.EAGER);
TextField lastName = new TextField();
lastName.setValueChangeMode(ValueChangeMode.EAGER);
TextField phone = new TextField();
phone.setValueChangeMode(ValueChangeMode.EAGER);
TextField email = new TextField();
email.setValueChangeMode(ValueChangeMode.EAGER);
DatePicker birthDate = new DatePicker();
Checkbox doNotCall = new Checkbox("Do not call");
Label infoLabel = new Label();
NativeButton save = new NativeButton("Save");
NativeButton reset = new NativeButton("Reset");
layoutWithBinder.addFormItem(firstName, "First name");
layoutWithBinder.addFormItem(lastName, "Last name");
layoutWithBinder.addFormItem(birthDate, "Birthdate");
layoutWithBinder.addFormItem(email, "E-mail");
FormItem phoneItem = layoutWithBinder.addFormItem(phone, "Phone");
phoneItem.add(doNotCall);
// Button bar
HorizontalLayout actions = new HorizontalLayout();
actions.add(save, reset);
save.getStyle().set("marginRight", "10px");
SerializablePredicate<String> phoneOrEmailPredicate = value -> !phone.getValue().trim().isEmpty() || !email.getValue().trim().isEmpty();
// E-mail and phone have specific validators
Binding<Contact, String> emailBinding = binder.forField(email).withValidator(phoneOrEmailPredicate, "Both phone and email cannot be empty").withValidator(new EmailValidator("Incorrect email address")).bind(Contact::getEmail, Contact::setEmail);
Binding<Contact, String> phoneBinding = binder.forField(phone).withValidator(phoneOrEmailPredicate, "Both phone and email cannot be empty").bind(Contact::getPhone, Contact::setPhone);
// Trigger cross-field validation when the other field is changed
email.addValueChangeListener(event -> phoneBinding.validate());
phone.addValueChangeListener(event -> emailBinding.validate());
// First name and last name are required fields
firstName.setRequiredIndicatorVisible(true);
lastName.setRequiredIndicatorVisible(true);
binder.forField(firstName).withValidator(new StringLengthValidator("Please add the first name", 1, null)).bind(Contact::getFirstName, Contact::setFirstName);
binder.forField(lastName).withValidator(new StringLengthValidator("Please add the last name", 1, null)).bind(Contact::getLastName, Contact::setLastName);
// Birthdate and doNotCall don't need any special validators
binder.bind(doNotCall, Contact::isDoNotCall, Contact::setDoNotCall);
binder.bind(birthDate, Contact::getBirthDate, Contact::setBirthDate);
// Click listeners for the buttons
save.addClickListener(event -> {
if (binder.writeBeanIfValid(contactBeingEdited)) {
infoLabel.setText("Saved bean values: " + contactBeingEdited);
} else {
BinderValidationStatus<Contact> validate = binder.validate();
String errorText = validate.getFieldValidationStatuses().stream().filter(BindingValidationStatus::isError).map(BindingValidationStatus::getMessage).map(Optional::get).distinct().collect(Collectors.joining(", "));
infoLabel.setText("There are errors: " + errorText);
}
});
reset.addClickListener(event -> {
// clear fields by setting null
binder.readBean(null);
infoLabel.setText("");
doNotCall.setValue(false);
});
infoLabel.setId("binder-info");
firstName.setId("binder-first-name");
lastName.setId("binder-last-name");
phone.setId("binder-phone");
email.setId("binder-email");
birthDate.setId("binder-birth-date");
doNotCall.setId("binder-do-not-call");
save.setId("binder-save");
reset.setId("binder-reset");
addCard("A form layout with fields using Binder", layoutWithBinder, actions, infoLabel);
}
use of com.vaadin.flow.component.checkbox.Checkbox in project flow-components by vaadin.
the class AbstractItemCountGridPage method initDataProvider.
private void initDataProvider() {
menuBar.add("Defined / Undefined size");
dataProvider = new LazyLoadingProvider();
Button button1 = new Button("withUndefinedSize() -> undefined size", event -> switchToUndefinedSize());
button1.setId(UNDEFINED_SIZE_BUTTON_ID);
menuBar.add(button1);
menuBar.add(new Hr());
Button button2 = new Button("setDataProvider(FetchCallback) -> undefined size", event -> switchToUndefinedSizeCallback());
menuBar.add(button2);
button2.setId("fetchcallback");
dataProviderSizeInput = new IntegerField("Fixed size backend size");
menuBar.add(dataProviderSizeInput, new Hr());
fetchCallbackSizeInput = new IntegerField("Undefined-size backend size", event -> fetchCallbackSize = event.getValue());
fetchCallbackSizeInput.setId(UNDEFINED_SIZE_BACKEND_SIZE_INPUT_ID);
fetchCallbackSizeInput.setWidthFull();
menuBar.add(fetchCallbackSizeInput, new Hr());
Button button3 = new Button("setDefinedSize(CountCallback) -> defined size", event -> switchToDefinedSize());
menuBar.add(button3);
button3.setId(DEFINED_SIZE_BUTTON_ID);
Button button4 = new Button("setDataProvider(DataProvider) -> defined size", event -> switchToDataProvider());
menuBar.add(button4);
button4.setId(DATA_PROVIDER_BUTTON_ID);
dataProviderSizeInput.setId(DATA_PROVIDER_SIZE_INPUT_ID);
dataProviderSizeInput.setValue(dataProviderSize);
dataProviderSizeInput.setWidthFull();
dataProviderSizeInput.addValueChangeListener(event -> {
dataProviderSize = event.getValue();
dataProvider.refreshAll();
});
dataProviderSizeInput.setEnabled(false);
menuBar.add(dataProviderSizeInput, new Hr());
Checkbox checkbox = new Checkbox("Show fetch query logs", event -> logPanel.setVisible(event.getSource().getValue()));
checkbox.setValue(true);
menuBar.add(checkbox);
}
use of com.vaadin.flow.component.checkbox.Checkbox in project flow-components by vaadin.
the class CheckboxDemoPage method addDefaultCheckbox.
private void addDefaultCheckbox() {
Checkbox checkbox = new Checkbox();
checkbox.setLabel("Default Checkbox");
checkbox.setId("default-checkbox");
NativeButton button = new NativeButton("Change label", event -> {
checkbox.setLabel("New Label");
});
button.setId("change-default-label");
addCard("Default Checkbox", checkbox, button);
}
use of com.vaadin.flow.component.checkbox.Checkbox in project flow-components by vaadin.
the class CheckboxDemoPage method addValueChangeCheckbox.
private void addValueChangeCheckbox() {
Checkbox valueChangeCheckbox = new Checkbox("Checkbox with a ValueChangeListener");
Div message = new Div();
valueChangeCheckbox.addValueChangeListener(event -> message.setText(String.format("Checkbox value changed from '%s' to '%s'", event.getOldValue(), event.getValue())));
addCard("Checkbox with a ValueChangeListener", valueChangeCheckbox, message);
valueChangeCheckbox.setId("value-change-checkbox");
message.setId("value-change-checkbox-message");
}
Aggregations