Get value from bootstrap gwt java datetimepicker

355 Views Asked by At

I'm trying to use a bootstrap3 datetimepicker in my gwt java web application, but I'm not sure if I'm doing everything ok, since it seems it doesn't pass the value of the field. Here is my code:

    datePicker = new DateTimePicker();
    datePicker.setFormat("dd MM yyyy hh ii"); 
    datePicker.setStartDate(new Date());
    datePicker.setLanguage(DateTimePickerLanguage.IT);
    Date expDate= datePicker.getValue();

Is that correct? Because it seems it is not passing any value. I also tried to add an event handler, like this:

        datePicker.addValueChangeHandler(new ValueChangeHandler<Date>() {
        @Override
        public void onValueChange(ValueChangeEvent<Date> event) {
             Date expDate= event.getValue();
             datePicker.setFormat("dd MM yyyy hh ii"); 
        }
       });

I can't understand if an event handler is necessary to capture the value of the datetimepicker, and if yes how can I use the expDate variable outside the scope of the method onValueChange, since I don't want to make it a global variable. Thanks.

Above the complete class (without the unnecessary code):

public class InsAst extends HTMLPanel {
private final AoLServiceAsync myAol = GWT.create(AoLService.class);
private DateTimePicker datePicker = new DateTimePicker();
private Date boxData;

public InsAst (final String cat) {

    super("");

    datePicker.setFormat("dd MM yyyy hh ii");
    datePicker.setStartDate(new Date());
    datePicker.setLanguage(DateTimePickerLanguage.IT);
    Label dataLabel = new Label("Exp date: ");
    dataLabel.setStyleName("control-label");
    dataLabel.getElement().getStyle().setProperty("float", "left");

    datePicker.addValueChangeHandler(new ValueChangeHandler<Date>() {
        public void onValueChange(ValueChangeEvent<Date> event) {
          boxData = event.getValue();
        }
      });

    objPanel.add(dataLabel);   
    objPanel.add(datePicker);

    addAstButton.addClickHandler(new ClickHandler() {
        public void onClick(ClickEvent event) {

            String user=MenuBar.getUsername(); 
            Date expDate= boxData;

            [...other code]

}
3

There are 3 best solutions below

0
On BEST ANSWER

I finally found that the problem was in this line of code:

datePicker.setFormat("dd MM yyyy hh ii");

Maybe this pass a wrong type of value incompatible with java Date.

3
On

Note that:

1
On

The onValueChange should have the value inside the received ValueChangeHandler.

You should probably call another method passing that date down instead of having it as global variable.

Without a more complete code example, hard to tell more than that.

PS: check GWT-Bootstrap's showcase for working samples as well: