Form adding listener to a field

Hi all,
How could I add a listener in a form, by example a checkbox (is a field in a form), i’m trying to do but I can not…

This is the code:

private FormWindow(boolean toaddregistres){
BeanItem<Pojo> classBean = new BeanItem<Pojo>(pojo);
setFormFieldFactory(new FormFieldFactory() {
				@Override
				public Field createField(Item item,Object propertyId, Component uiContext) {
				workcheckbox = new CheckBox(app.locale.getString("PUNISHMENTSLAYOUT_WINDOW_FORM_CHECKBOX_WORK_CAPTION"));	
				workcheckbox.setRequired(false);
[color=#ee0000]
                                workcheckbox.addListener(new Property.ValueChangeListener() {			
									@Override
									public void valueChange(ValueChangeEvent event) {
										// TODO Auto-generated method stub
										
									}
								});
[/color]

..........
							

The highlight no works :frowning:
I would do that when the checkbox is checked other field(textfield) is enabled and when isn’t checked the textfield isn enabled.

Thanks!!
Xavier

One possible problem with your code could be that the workcheckbox is not in immediate mode. Try to add workcheckbox.setImmediate(true) call in your code. (of course you have to call setEnable(false) for the textfield that you want to be disabled)

Is there any other problems with that code, does it compile, is the checkbox shown in the ui etc.?

Do no works with inmediateç(true)
The checkbox works and is showing but the problem is that I can’t add a listener.

This code doesn’t compile.


						}else if(propertyId.equals(DatabaseStorage.WORK_PROPERTY)){
								workcheckbox = new CheckBox(app.locale.getString("PUNISHMENTSLAYOUT_WINDOW_FORM_CHECKBOX_WORK_CAPTION"));	
								workcheckbox.setRequired(false);
								workcheckbox.setImmediate(true);
								workcheckbox.addListener(new Property.ValueChangeListener() {
									
									@Override
									public void valueChange(ValueChangeEvent event) {
										// TODO Auto-generated method stub
										
									}
								});
								return workcheckbox;
					     }else{

But this compile


						}else if(propertyId.equals(DatabaseStorage.WORK_PROPERTY)){
								workcheckbox = new CheckBox(app.locale.getString("PUNISHMENTSLAYOUT_WINDOW_FORM_CHECKBOX_WORK_CAPTION"));	
								workcheckbox.setRequired(false);
								workcheckbox.setImmediate(true);
								return workcheckbox;
					     }else{

I think there are two possible reasons for the compilation error:[list]

[*]
You have missing imports in your Java file, make sure that you have the following imports:

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

[*]
Your project uses Java version 5 or older, when you cannot use the @Override annotation when implementing a method in an interface. Remove the annotation or use Java 6 to fix the error.

[/list]

Could you provide the compiler error?

Hi thanks you in advance
The imports were added.
The java version is 6

And the code on left side displays this :

Line “workcheckbox.addListener(new Property.ValueChangeListener() {”

Multiple markers at this line
- The serializable class does not declare a static final serialVersionUID field of type long
- The type new Property.ValueChangeListener(){} must implement the inherited abstract method
Property.ValueChangeListener.valueChange(Property.ValueChangeEvent)


And in line public void valueChange(ValueChangeEvent event) {

The method valueChange(Field.ValueChangeEvent) of type new Property.ValueChangeListener(){} must override or implement a supertype method

[u]
[color]

[/color]If i remove the Override annotadion :
[/u]


The line “workcheckbox.addListener(new Property.ValueChangeListener() {” displays

Multiple markers at this line
- The type new Property.ValueChangeListener(){} must implement the inherited abstract method
Property.ValueChangeListener.valueChange(Property.ValueChangeEvent)
- The serializable class does not declare a static final serialVersionUID field of type long

Hmm. Does it work if you replace line 9 “public void valueChange(ValueChangeEvent event) {” with following?
public void valueChange(Property.ValueChangeEvent event) {

If not, could you provide all imports of this class?

This is just warning, that could be fixed adding mentioned field into the class.

This makes me suspect that somehow you have wrong value change method here. But I couldn’t check this at the moment.

When I compile console displays:


com.vaadin.event.ListenerMethod$MethodException
Cause: java.lang.Error: Unresolved compilation problems: 
	The type new Property.ValueChangeListener(){} must implement the inherited abstract method Property.ValueChangeListener.valueChange(Property.ValueChangeEvent)
	The method valueChange(Field.ValueChangeEvent) of type new Property.ValueChangeListener(){} must override or implement a supertype method

	at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:507)
	at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:161)
	at com.vaadin.ui.AbstractComponent.fireEvent(AbstractComponent.java:1154)
	at com.vaadin.ui.Button.fireClick(Button.java:371)
	at com.vaadin.ui.Button.changeVariables(Button.java:193)
	at com.vaadin.terminal.gwt.server.AbstractCommunicationManager.handleVariables(AbstractCommunicationManager.java:1094)
	at com.vaadin.terminal.gwt.server.AbstractCommunicationManager.doHandleUidlRequest(AbstractCommunicationManager.java:590)
	at com.vaadin.terminal.gwt.server.CommunicationManager.handleUidlRequest(CommunicationManager.java:266)
	at com.vaadin.terminal.gwt.server.AbstractApplicationServlet.service(AbstractApplicationServlet.java:476)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
	at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
	at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.Error: Unresolved compilation problems: 
	The type new Property.ValueChangeListener(){} must implement the inherited abstract method Property.ValueChangeListener.valueChange(Property.ValueChangeEvent)
	The method valueChange(Field.ValueChangeEvent) of type new Property.ValueChangeListener(){} must override or implement a supertype method

And the code is :[code]

private FormWindow(){
BeanItem classBean = new BeanItem(pojo);
groupscombo = new ComboBox(app.locale.getString(“PUNISHMENTSLAYOUT_WINDOW_FORM_COMBO_GROUPS_CAPTION”));
setFormFieldFactory(new FormFieldFactory() {
@Override
public Field createField(Item item,Object propertyId, Component uiContext) {
if(propertyId.equals(DatabaseStorage.WORKDESCRIPTION_PROPERTY)){
textfield2=new TextField();
textfield2.addValidator(new StringLengthValidator(locale.getString(“PUNISHMENTSLAYOUT_WINDOW_FORM_WORKDESCRIPTION_ERROR_VALUE_TOOLARGE”),1,Constants.LONGEST_VARCHAR_LENGTH,false));
textfield2.setRequired(false);
textfield2.setRows(5);
textfield2.setCaption(app.locale.getString(“PUNISHMENTSLAYOUT_WINDOW_WORKDESCRIPTION_CAPTION”));
return textfield2;
}else if(propertyId.equals(DatabaseStorage.WORK_PROPERTY)){
workcheckbox = new CheckBox(app.locale.getString(“PUNISHMENTSLAYOUT_WINDOW_FORM_CHECKBOX_WORK_CAPTION”));
workcheckbox.setRequired(false);
workcheckbox.addListener( new Property.ValueChangeListener() {

								@Override
								public void valueChange(ValueChangeEvent event) {
									// TODO Auto-generated method stub
									
								}});
							return workcheckbox;
				     }else{
				    	 return null; 
					 }
				}
			});

[/code]
And the imports :


import com.vaadin.data.Container;
import com.vaadin.data.Item;
import com.vaadin.data.Property;
import com.vaadin.data.Validator;
import com.vaadin.data.Container.ItemSetChangeEvent;
import com.vaadin.data.Validator.InvalidValueException;
import com.vaadin.data.util.BeanItem;
import com.vaadin.data.util.IndexedContainer;
import com.vaadin.data.util.QueryContainer;
import com.vaadin.data.validator.StringLengthValidator;
import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.CheckBox;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Component;
import com.vaadin.ui.CssLayout;
import com.vaadin.ui.CustomField;
import com.vaadin.ui.DefaultFieldFactory;
import com.vaadin.ui.Field;
import com.vaadin.ui.Form;
import com.vaadin.ui.FormFieldFactory;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.InlineDateField;
import com.vaadin.ui.Label;
import com.vaadin.ui.ListSelect;
import com.vaadin.ui.OptionGroup;
import com.vaadin.ui.Panel;
import com.vaadin.ui.PopupDateField;
import com.vaadin.ui.Table;
import com.vaadin.ui.TextField;
import com.vaadin.ui.TwinColSelect;
import com.vaadin.ui.Upload;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
import com.vaadin.ui.AbstractSelect.Filtering;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Upload.FailedEvent;
import com.vaadin.ui.Upload.SucceededEvent;
import com.vaadin.ui.Window.Notification;
import com.vaadin.ui.themes.Reindeer;

I tryied to reproduce your problem, and found one possible solution. At least following little bit modified version of your code seem to work well. It seems that Vaadin innerclasses are producing problems if the package names are not excatly correct (like here “com.vaadin.data.Property.ValueChangeEvent event”)…


import com.vaadin.data.Item;
import com.vaadin.data.Property;
import com.vaadin.ui.CheckBox;
import com.vaadin.ui.Component;
import com.vaadin.ui.Field;
import com.vaadin.ui.Form;
import com.vaadin.ui.FormFieldFactory;
import com.vaadin.ui.TextField;

public class FormWindow extends Form {
	private TextField textfield2;
	private CheckBox workcheckbox;

	public FormWindow() {
		setFormFieldFactory(new FormFieldFactory() {
			public Field createField(Item item, Object propertyId, Component uiContext) {
				if (propertyId.equals("aaa")) {
					textfield2 = new TextField();
					textfield2.setRequired(false);
					textfield2.setRows(5);
					textfield2.setCaption("qwerty");
					return textfield2;
				} else if (propertyId.equals("bbb")) {
					workcheckbox = new CheckBox("qwerty");
					workcheckbox.setRequired(false);
					workcheckbox.setImmediate(true);
					workcheckbox.addListener(new Property.ValueChangeListener() {
						public void valueChange(com.vaadin.data.Property.ValueChangeEvent event) {
							System.out.println("value changed");
						}
					});
					return workcheckbox;
				} else {
					return null;
				}
			}
		});
	}
}

Yes, this is an Eclipse problem - if I remember correctly, it does not import inner interfaces correctly (in some cases - maybe when the workspace contains multiple similarly named inner interfaces?) upon auto-import or code completion. Furthermore, the error messages caused by this are not clear.

I have written about this before, and if I have the time and remember to investigate this further, I could check if it is already in the Eclipse JDT bug database.

Workarounds: explicitly write “Property.ValueChangeEvent”, “Property.ValueChangeListener” etc. or import them by hand.


Thank you so much.

Now it works properly!