PDA

View Full Version : combobox with dates



francesco.calabri
4 Jan 2011, 7:47 AM
Hi all,
I'm trying to render a combo box wich displays values like <Month, Year> since Januray, 3 years ago to this month this year.
I'm able to reach that with a SimpleComboBox<String> like that:


SimpleComboBox<String> months = new SimpleComboBox<String>();
months.setFieldLabel("Mese");
months.setTriggerAction(TriggerAction.ALL);
formPanel.add(months); //formPanel is a FormPanel

AsyncCallback<List<String>> callback = new AsyncCallback<List<String>>() {

@Override
public void onFailure(Throwable caught) {
// TODO Auto-generated method stub
}

@Override
public void onSuccess(List<String> result) {
if (result != null && result.size() > 0) {
months.add(result);
months.select(months.findModel(result.get(0)));
}
}
};
provider.getMonths(callback);
and the servlet method getMonths is...


public List<String> getMonths() {
Calendar c = Calendar.getInstance();
int actMonth = c.get(Calendar.MONTH);
int actYear = c.get(Calendar.YEAR);
int minMonth = 11;
int minYear = actYear-3;
List<String> list = new ArrayList<String>();
boolean compute = true;
while (compute) {
list.add(monthsString[actMonth]+" "+actYear);
if (actMonth == 0) {
actMonth = 11;
actYear -= 1;
} else actMonth -= 1;
if (actMonth == minMonth && actYear == minYear)
compute = false;
}
return list;
}
But I would prefer a lot to work with java,.util.Date, and have something like a TimeField but for dates.
So which is the easyest way to get what I want?

Thanks,
F

sven
4 Jan 2011, 11:31 AM
You can do this with ComboBox. You would wrap the java.util.Date in a ModelData instance. The template could than format the date.