首页 > 解决方案 > 使用 Vaadin Designer 时 Vaadin 8 绑定到 Pojo

问题描述

我正在开发一个从 Vaadin 设计器生成 UI 的项目。我有生成的 html 和相应的 .java 文件。

我扩展了 java 文件并将我的数据绑定到设计视图。显示我的数据的基本代码正在工作,因为表单上的每个字段都有一个相应的 pojo 变量。现在我想在表单上同时显示两个字段,就像您将名字和姓氏结合起来显示全名一样。

我的 pojo 有一个 toString 方法,它返回组合的两个值,但我不知道如何将标签 propertyNoName 设置为 aPropertyProfile.toString() 的值

public class PropertyProfileDesign extends VerticalLayout
{

    private VerticalLayout  propertyProfileHeader;
    private Label           propertyNoName;
    private TextField       propertyMgr;
    private TextField       propertyMgrPhone;
    private TextField       propertyMgrCellular;
    private TextField       propertyMgrHome;
    private TextField       regionalMgr;
    private TextField       regionalMgrPhone;
    private TextField       regionalMgrCellular;
    private TextField       regionalMgrHome;
    private VerticalLayout  propertyProfileBodyFooter;

    public PropertyProfileDesign()
    {
        Design.read( this );
    }

    public VerticalLayout getPropertyProfileHeader()
    {
        return propertyProfileHeader;
    }

    public Label getPropertyNoName()
    {
        return propertyNoName;
    }

    public TextField getPropertyMgr()
    {
        return propertyMgr;
    }

    public TextField getPropertyMgrPhone()
    {
        return propertyMgrPhone;
    }

    public TextField getPropertyMgrCellular()
    {
        return propertyMgrCellular;
    }

    public TextField getPropertyMgrHome()
    {
        return propertyMgrHome;
    }

    public TextField getRegionalMgr()
    {
        return regionalMgr;
    }

    public TextField getRegionalMgrPhone()
    {
        return regionalMgrPhone;
    }

    public TextField getRegionalMgrCellular()
    {
        return regionalMgrCellular;
    }

    public TextField getRegionalMgrHome()
    {
        return regionalMgrHome;
    }

    public VerticalLayout getPropertyProfileBodyFooter()
    {
        return propertyProfileBodyFooter;
    }
}

public class PropertyProfileView extends PropertyProfileDesign
{

    private static final long serialVersionUID = 7454308982717559970L;

    private Binder<PropertyProfile> binder = new Binder<>( PropertyProfile.class );

    public PropertyProfileView()
    {
    }

    public PropertyProfileView( final PropertyProfile aPropertyProfile )
    {
        this();

        binder.bindInstanceFields( this );
        binder.readBean( aPropertyProfile );
        //  How to update the label field propertyNoName with aPropertyProfile.toString()?

    }
}

标签: javavaadinvaadin8

解决方案


只需使用 Label 的 setValue(..) 方法:

propertyNoName.setValue(aPropertyProfile.toString());

推荐阅读