DateField listener

I’m trying to add a listener for a datefield so that i can check for valid input in the field, but so far i haven’t been able to write functional code. i’m trying to mirror a listener that i have for a textfield but instead of TextChangeListener I was using ValueChangeListener. All of this is saved on my work computer so I don’t have a code example right now.

What do you mean by not having functional code? Do you have the right syntax but it just doesn’t work? In that case make sure you added setImmediate(true) to your Datefield.

I mean that it does the same thing as it did before I added the listener. But I can’t figure out what I’m doing wrong. This is what I tried using

final DateField date = new DateField();
                date.setDateFormat("MM-dd-yyyy");
                date.setWidth("105px");
                date.setImmediate(true);
                date.addListener(new Field.ValueChangeListener(){
                    @Override
                    public void valueChange(ValueChangeEvent event) {
                        date.setValue(event.getProperty());
                }
            });
                return date;

So what should it actually do? because what it should do from your code is that when you change the value, it gets set again to the same value it is already on. If you use it and you can’t see anything noticeable happening when you change the value this is the correct behavior. You could add System.out.println(“Value change called.”); to the valuechange void to at least see something in the logs.

I want the field to turn red if the format is incorrect and also i have a start date and an end date and I want to be able to check that the end date is not before the start date

…and how should date.setValue(event.getProperty()) do all of that? In the valueChange void you can do all calculations and check whether the date matches the format and the start/end date value and then react accordingly for example by setting a red styling to the field.
Date.setValue on the other hand is in your case not really doing anything at all.

This sounds like a job for a Validator to me…

I wasn’t sure if there was a particular way that I should have approached it. I started using vaadin 3 days ago and I’m still figuring things out. I ended up using an error pop up instead. As it turned out date.setValue created an internal error anyway so i just got rid of that and made calls to a setter and a method i wrote in another class to perform those checks.

how does a validator help? i’m a n00b

For Validators, have a look at
this example
,
this api reference
and
this unfinished wiki article
.