Errors in addListener method

Hello.

I do exercises from the book “Book of Vaadin”.
I don’t know why there are errors in the next example:


import com.vaadin.Application;
import com.vaadin.ui.*;

CheckBox switchEditable = new CheckBox("Editable");
switchEditable.addListener(new Property.ValueChangeListener() {
	public void valueChange(ValueChangeEvent event) {			
	}
	});

Errors:

  1. The method addListener(Button.ClickListener) in the type Button is not applicable for the arguments (new ValueChangeListener(){})
  2. Property cannot be resolved to a type
  3. ValueChangeEvent cannot be resolved to a type

I think, I have to import something. But I don’t know what.

import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;

I usually write it in form:

switchEditable.addListener(new ValueChangeListener() {
    public void valueChange(ValueChangeEvent event) {
    }
});

(missing “Property.” in Listener)
which means that the import would be:

import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;

Guessing from your error messages, I’d guess you are using Eclipse, correct? In eclipse you can press ctrl+shift+o, which tries to resolve the imports, and gives you an list of possibilities if there are multiple that match. Very handy tool.

Eclipse has a long standing bug in auto-import of nested interfaces or classes in specific cases (if I remember correctly, when there are multiple possibilities of certain kinds for the nested interface), and unfortunately this happens with a lot of Vaadin listeners written as anonymous classes. This is complicated by the fact that the error message you get is not very good. You are especially likely to see the issue if you have configured “organize imports” as a “save action” for Java files.

Explicit “Organize imports…” can often fix this as Jens suggested, as can manually adding imports for the correct classes (either Property in your example or Property.ValueChangeListener and then removing “Property.” when declaring the listener anonymous class).

Thank you very much for your help.
Yes, I use Eclipse. Your advice using ctrl+shift+o is very useful for me.
Thank you again.