setVisibleItemProperties没有效果VAADIN

时间:2013-02-12 17:02:36

标签: java web vaadin

我使用了Vaadin电话簿示例&试图做一些修改并制作一个新的应用程序。我需要更改表单字段的名称,我将其更改为如下所示。但是之后没有显示任何项目。添加

后,只有2个按钮显示在那里
        editorForm.setVisibleItemProperties(new Object[] { "First Name",
            "Last Name", "Phone Number", "Street", "City", "Postal Code",
            "Brand/Item" });

line.i在setItemDataSource之前和setItemDataSource之后输入它。但同样的结果。当我删除上面的行时,表单将正确弹出并显示所有都是corectly

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);
}

}

1 个答案:

答案 0 :(得分:2)

setVisibleItemProperties方法用于定义表单中的可见字段,而不是为字段定义字幕。

以下是为表单中的字段设置标题的一个示例:

form.getField("firstName").setCaption("First Name");

另一种方法是创建自己的FieldFactory,为字段设置字幕(例如,基于注释)。