0

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]

}
JamieITGirl
  • 161
  • 1
  • 11

3 Answers3

1

Note that:

Sal
  • 1,307
  • 1
  • 8
  • 16
  • I noticed that in every sample I've found the DateTimePicker it's used inside an onModuleLoad() method, e.g. http://www.gwtproject.org/javadoc/latest/com/google/gwt/user/datepicker/client/DatePicker.html But how can I use it if I'm not in a onModuleLoad() method? This, I haven't found. – JamieITGirl Jan 19 '18 at 11:32
  • Well, I tried to use the DateBox instead of DateTimePicker and it seems to work...and without adding any event handler. I really don't know why. But datebox picks only the date...I also need the hour :( – JamieITGirl Jan 19 '18 at 12:42
  • There is a `DateTimeBox`. – Sal Jan 19 '18 at 15:34
0

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:

caarlos0
  • 20,020
  • 27
  • 85
  • 160
  • Thank you very much Carlos, I've just edited my message adding the complete code...What do you mean with "The onValueChange should have the value inside the received ValueChangeHandler"? – JamieITGirl Jan 19 '18 at 00:19
0

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.

JamieITGirl
  • 161
  • 1
  • 11