jcovert
jcovert

Reputation: 550

GWT DatePicker to recognize Calendar Icon click

I'm using a DatePicker widget, and I put a little calendar image next to it. When you click on the actual DatePicker textbox, the calendar popup pops up (as it should). I'm looking to add this click handling to the little calendar image too, so that the user can click either the box or the image to see the calendar popup. Is there an easy way to do this?

I thought it might be something like this:

calendarImage.addClickHandler(datePick.getTextBox().getClickHandler())

But it doesn't seem that anything like this exists.

Thanks in advance!

Upvotes: 0

Views: 3948

Answers (2)

Thomas Broyer
Thomas Broyer

Reputation: 64541

Assuming you mean DateBox when you write DatePicker, simply call showDatePicker on click of the icon (and/or test isDatePickerShowing and call hideDatePicker)

Upvotes: 2

tim_a
tim_a

Reputation: 950

Just add a clickhandler to the image that sets the datepicker visible.

It will become something like this:

private DatePicker datePicker = new DatePicker();
private TextBox textBox = new TextBox();
private Image icon = new Image("calendar.png");

public void onModuleLoad() {

    datePicker.addValueChangeHandler(new ValueChangeHandler<Date>() {

        @Override
        public void onValueChange(ValueChangeEvent<Date> event) {
            Date date = event.getValue();
            String dateStr = DateTimeFormat.getFormat(DateTimeFormat.PredefinedFormat.DATE_MEDIUM).format(date);
            textBox.setText(dateStr);
            datePicker.setVisible(false);
        }
    });
    datePicker.setVisible(false);

    icon.addClickHandler(new ClickHandler() {

        @Override
        public void onClick(ClickEvent event) {
            datePicker.setVisible(true);        
        }
    });

    RootPanel.get().add(icon);
    RootPanel.get().add(textBox);
    RootPanel.get().add(datePicker);
}

Upvotes: 3

Related Questions