Search code examples
gwtdatepickergwt2uibinder

datePicker in GWT


Hi have Date in a string Format.

datevalue= [Tue Mar 06 06:00:00 CET 2012].

in my code i have textBox where value is set. when i click on textbox i see a datepicker.

what i want to do is , In a datepicker the date should be highlighted whatever the value is in textbox.

and also when i click datePicker the value in the text should be in a same format as [Tue Mar 06 06:00:00 CET 2012].

my code is.

private TextBox getTimeLable(String datevalue) {
        // TODO Auto-generated method stub
        System.out.println("date" + datevalue);
        final TextBox   timebx =new TextBox();
        timebx.setReadOnly(true);
        timebx.setText(datevalue);
        final PopupPanel popupPanel=new PopupPanel(true);
        DatePicker datePicker=new DatePicker();
        datePicker.addValueChangeHandler(new ValueChangeHandler<Date>() {

            public void onValueChange(ValueChangeEvent<Date> event) {
                // TODO Auto-generated method stub

                Date date=event.getValue();
                timebx.setText(DateTimeFormat.getFormat("yyyy-MM-dd").format(date));
                popupPanel.hide();
            }
        });
        popupPanel.setWidget(datePicker);
        timebx.addClickHandler(new ClickHandler() {

            public void onClick(ClickEvent event) {
                // TODO Auto-generated method stub
                int x=timebx.getAbsoluteLeft();
                int y=timebx.getAbsoluteTop();
                popupPanel.setPopupPosition(x, y+20);
                popupPanel.show();
            }
        });

        return timebx;
    }

edited for error

errorjava.lang.IllegalArgumentException: [Tue Mar 06 05:00:00 UTC 2012]

errorjava.lang.IllegalArgumentException: [Wed May 09 12:00:00 UTC+2 2012]

Initially the textbox is with this value [Tue Mar 06 05:00:00 UTC 2012] . but when i click on text box n select the date . date picker gives the second date in textbox Wed May 09 12:00:00 UTC+2 2012. The first one may be illegalArgument as it is set by me. but the second one is set by the date picker.


Solution

  • This should work

    import com.google.gwt.i18n.client.DateTimeFormat; 
    
    DatePicker datePicker=new DatePicker();  // Have to make final to use in inner method
    
    
    timebx.addClickHandler(new ClickHandler() {
    
            public void onClick(ClickEvent event) {
                // TODO Auto-generated method stub
                String strDate = timebx.getText();
                DateTimeFormat format = DateTimeFormat.getFormat("yyyy-MM-dd");
                try {  
                  Date selDate = (Date)format.parse(strDate); 
                  datePicker.setValue(selDate, true);
                } catch(Exception pe){
                 // setting current date
                 datePicker.setValue(new Date(), true);
                }
    
                int x=timebx.getAbsoluteLeft();
                int y=timebx.getAbsoluteTop();
                popupPanel.setPopupPosition(x, y+20);
                popupPanel.show();
            }
        });