Skip to content

Instantly share code, notes, and snippets.

@johannest
Created August 22, 2023 11:07
Show Gist options
  • Save johannest/cfbfde8e1af1db58001dedd8560db46e to your computer and use it in GitHub Desktop.
Save johannest/cfbfde8e1af1db58001dedd8560db46e to your computer and use it in GitHub Desktop.
Ex.9
package com.vaadin.training.framework.exercises.ex9;
import com.vaadin.data.util.IndexedContainer;
import com.vaadin.navigator.View;
import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.TextField;
@SuppressWarnings("serial")
public class DataBinding extends GridLayout implements View {
public DataBinding() {
super(2, 2);
setSpacing(true);
TextField textField = new TextField();
textField.setImmediate(true);
addComponent(textField);
Label label1 = new Label();
label1.setPropertyDataSource(textField);
addComponent(label1);
ComboBox comboBox = createCombo1();
/**
* Uncomment the following two lines for the bonus solution.
* Note that <code>comboBox.setPropertyDataSource(textField);</code> is
* not a correct solution as it will not sanitize the textfield input
* properly with respect to the comboBox.
*/
textField.setPropertyDataSource(comboBox);
textField.setNullRepresentation("");
addComponent(comboBox);
Label label2 = new Label();
label2.setPropertyDataSource(comboBox);
addComponent(label2);
}
@SuppressWarnings("unchecked")
private ComboBox createCombo1() {
IndexedContainer cont = new IndexedContainer();
cont.addContainerProperty("caption", String.class, "");
cont.addItem("DE").getItemProperty("caption").setValue("Germany");
cont.addItem("FI").getItemProperty("caption").setValue("Finland");
cont.addItem("US").getItemProperty("caption").setValue("USA");
ComboBox comboBox = new ComboBox();
comboBox.setContainerDataSource(cont);
comboBox.setItemCaptionPropertyId("caption");
comboBox.setImmediate(true);
return comboBox;
}
private ComboBox createCombo2() {
ComboBox comboBox = new ComboBox();
comboBox.addItem("DE");
comboBox.addItem("FI");
comboBox.addItem("US");
comboBox.setItemCaption("DE", "Germany");
comboBox.setItemCaption("FI", "Finland");
comboBox.setItemCaption("US", "USA");
comboBox.setImmediate(true);
return comboBox;
}
@Override
public void enter(ViewChangeEvent event) {
//Navigator method, ignore for now
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment