我使用了 Vaadin 电话簿示例并尝试进行一些修改并创建一个新应用程序。我需要更改表单字段的名称,我将其更改如下。但之后它没有显示任何项目。添加后仅显示 2 个按钮
editorForm.setVisibleItemProperties(new Object[] { "First Name",
"Last Name", "Phone Number", "Street", "City", "Postal Code",
"Brand/Item" });
line.i 在 setItemDataSource 之前和 setItemDataSource line.but 之后输入它。但结果相同。当我删除上面的行时,表单将正确弹出并显示所有内容都是正确的
package com.vaadin.demo.jpaaddressbook;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Vector;
import com.vaadin.addon.beanvalidation.BeanValidationForm;
import com.vaadin.data.Item;
import com.vaadin.demo.jpaaddressbook.domain.Person;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Component;
import com.vaadin.ui.DefaultFieldFactory;
import com.vaadin.ui.Field;
import com.vaadin.ui.Form;
import com.vaadin.ui.FormFieldFactory;
import com.vaadin.ui.TextField;
import com.vaadin.ui.Window;
@SuppressWarnings("serial")
public class PersonEditor extends Window implements Button.ClickListener,
FormFieldFactory {
private final Item personItem;
private Form editorForm;
private Button saveButton;
private Button cancelButton;
public PersonEditor(Item personItem) {
this.personItem = personItem;
editorForm = new BeanValidationForm<Person>(Person.class);
editorForm.setFormFieldFactory(this);
editorForm.setWriteThrough(false);
editorForm.setImmediate(true);
editorForm.setVisibleItemProperties(new Object[] { "First Name",
"Last Name", "Phone Number", "Street", "City", "Postal Code",
"Brand/Item" });
editorForm.setItemDataSource(personItem, Arrays.asList("firstName",
"lastName", "phoneNumber", "street", "city", "zipCode",
"department"));
saveButton = new Button("Save", this);
cancelButton = new Button("Cancel", this);
editorForm.getFooter().addComponent(saveButton);
editorForm.getFooter().addComponent(cancelButton);
getContent().setSizeUndefined();
addComponent(editorForm);
setCaption(buildCaption());
}
/**
* @return the caption of the editor window
*/
private String buildCaption() {
return "Enter Contact Details";
}
/*
* (non-Javadoc)
*
* @see com.vaadin.ui.Button.ClickListener#buttonClick(com.vaadin.ui.Button.
* ClickEvent)
*/
@Override
public void buttonClick(ClickEvent event) {
if (event.getButton() == saveButton) {
editorForm.commit();
fireEvent(new EditorSavedEvent(this, personItem));
} else if (event.getButton() == cancelButton) {
editorForm.discard();
}
close();
}
/*
* (non-Javadoc)
*
* @see com.vaadin.ui.FormFieldFactory#createField(com.vaadin.data.Item,
* java.lang.Object, com.vaadin.ui.Component)
*/
@Override
public Field createField(Item item, Object propertyId, Component uiContext) {
if ("department".equals(propertyId)) {
return new DepartmentSelector();
}
Field field = DefaultFieldFactory.get().createField(item, propertyId,
uiContext);
if (field instanceof TextField) {
((TextField) field).setNullRepresentation("");
}
return field;
}
public void addListener(EditorSavedListener listener) {
try {
Method method = EditorSavedListener.class.getDeclaredMethod(
"editorSaved", new Class[] { EditorSavedEvent.class });
addListener(EditorSavedEvent.class, listener, method);
} catch (final java.lang.NoSuchMethodException e) {
// This should never happen
throw new java.lang.RuntimeException(
"Internal error, editor saved method not found");
}
}
public void removeListener(EditorSavedListener listener) {
removeListener(EditorSavedEvent.class, listener);
}
public static class EditorSavedEvent extends Component.Event {
private Item savedItem;
public EditorSavedEvent(Component source, Item savedItem) {
super(source);
this.savedItem = savedItem;
}
public Item getSavedItem() {
return savedItem;
}
}
public interface EditorSavedListener extends Serializable {
public void editorSaved(EditorSavedEvent event);
}
}