Search code examples
javavaadinvaadin7datefield

Select "Today" in Vaadin’s calendar widgets, DateField and InlineDateField?


Screen-shot of Vaadin InlineDateField widget

Vaadin has a pair of nice calendar widgets, DateField & InlineDateField.

One feature I've not detected: Can the user get back to "Today" after perusing various months and dates?

Or must I add my own separate "Today" button? At least I could do so for InlineDateField, but not DateField.


Solution

  • I don't believe so, I think you would have to code it yourself.

    I think its the same for JodaTime too.

    I'm sure you already figured this out, but here is some simple code for anyone else!

        final DateField x = new DateField();
    
        final InlineDateField y = new InlineDateField();
    
        HorizontalLayout layout = new HorizontalLayout();
        layout.setSpacing(true);
    
        layout.addComponent(x);
        layout.addComponent(y);
    
        Button button = new Button("Today");
    
        layout.addComponent(button);
    
        button.addClickListener(new Button.ClickListener() {
            public void buttonClick(ClickEvent event) {
                Date date = new Date();
                x.setValue( date );
                y.setValue( date );
            }
        });
    
    
    
        this.setContent(layout);