Vaadin – set Age into TextField from value Date – Popup PopupDateField

Vaadin – set Age value into TextField from value Date – Popup PopupDateField

datetoAge

1). First, create Simple vaadin project , just right click create Vaadin 7 Project (Maven).

2).  Class MyUI.java , declare Components PopupDateField – dateBirth , and TextField – ageField.

3). Source code MyUI.java.

@Theme("mytheme")
@Widgetset("com.example.testVaadin.MyAppWidgetset")
public class MyUI extends UI {
 private PopupDateField dateBirth;
 private String strAge;
 private TextField ageField;
 private FormLayout formLayout;
 private FieldGroup binder;
//Have an item
PropertysetItem item;
 @Override
 protected void init(VaadinRequest vaadinRequest) {
 final VerticalLayout layout = new VerticalLayout();
 formLayout = new FormLayout();
 // date PopupField
 dateBirth = new PopupDateField("Date :");
 dateBirth.setDateFormat("dd-MM-yyyy");
 dateBirth.addStyleName("tiny");
 dateBirth.setWidth("10em");
 // age textField
 ageField = new TextField("Age :");
 ageField.addStyleName("tiny");
 ageField.setWidth("10em");
 listenerComponent();
 
 formLayout.addComponents(dateBirth,ageField);
 layout.addComponents(formLayout);
 layout.setMargin(true);
 layout.setSpacing(true);
 
 setContent(layout);
 }

 @WebServlet(urlPatterns = "/*", name = "MyUIServlet", asyncSupported = true)
 @VaadinServletConfiguration(ui = MyUI.class, productionMode = false)
 public static class MyUIServlet extends VaadinServlet {
 }
 
 @SuppressWarnings("deprecation")
 public void listenerComponent() {
 item = new PropertysetItem();
 
 dateBirth.addValueChangeListener(new PopupDateField.ValueChangeListener() {
 @Override
 public void valueChange(ValueChangeEvent event) {
 strAge = getPersonAge((Date)event.getProperty().getValue());
 System.out.println(" ageBirthDate : " + strAge); 
 item.addItemProperty("age", new ObjectProperty<String>(strAge));
 binder = new FieldGroup(item);
 binder.bind(ageField, "age");
 }
 });
 
 }
 
 public String getPersonAge(Date birthDate) {
 // convert date to stringDateFormat MM-dd-yyyy
 SimpleDateFormat formatter = new SimpleDateFormat("MM-dd-yyyy");
 // convert simpleDateFormat to string
 String dateStr = formatter.format(birthDate);
 System.out.println("date String : " + dateStr);
 
 Calendar birth = new GregorianCalendar();
 Calendar today = new GregorianCalendar();
 int age = 0;
 int factor = 0; 
//calculate age
 try {

 Date birthDt = new SimpleDateFormat("MM-dd-yyyy").parse(dateStr);
 Date currentDate = new Date(); //current date
 
 birth.setTime(birthDt);
 today.setTime(currentDate);
 
 // check if birthday has been celebrated this year
 if(today.get(Calendar.DAY_OF_YEAR) < birth.get(Calendar.DAY_OF_YEAR)) {
 factor = -1;
 //birthday not celebrated
 }
 age = today.get(Calendar.YEAR) - birth.get(Calendar.YEAR) + factor;
 } catch (ParseException e) {
    e.printStackTrace();
 } 

 return String.valueOf(age);
 }

 

4) I Used Simple Binding to bind to Field Age TextField from value Date Component PopupDateField .

PropertysetItem item = new PropertysetItem();
// remove item property by Object id "age"
item.removeItemProperty("age");
item.addItemProperty("age", new ObjectProperty<String>(strAge));
FieldGroup  binder = new FieldGroup(item);
binder.bind(ageField, "age");

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s