Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 是否在组合框中显示两个或多个属性?_Java_Vaadin7 - Fatal编程技术网

Java 是否在组合框中显示两个或多个属性?

Java 是否在组合框中显示两个或多个属性?,java,vaadin7,Java,Vaadin7,我试图在一个组合框中显示两个属性。我尝试了setItemCaptionPropertyId(),但这只显示nome,我想显示nome和SobreName或更多属性 我正在试这个 //jpacontainer aluno private CustomJPAContainer<Aluno> dsAluno = new CustomJPAContainer<Aluno>(Aluno.class); //combobox aluno ComboBox cbxAl

我试图在一个组合框中显示两个属性。我尝试了
setItemCaptionPropertyId()
,但这只显示
nome
,我想显示
nome
SobreName
或更多属性

我正在试这个

//jpacontainer aluno
private CustomJPAContainer<Aluno> dsAluno = new CustomJPAContainer<Aluno>(Aluno.class);

//combobox aluno
        ComboBox cbxAluno = (ComboBox)field;
        cbxAluno.setItemCaptionMode(ItemCaptionMode.PROPERTY);
        cbxAluno.setConverter(new SingleSelectConverter<Aluno>(cbxAluno));
        cbxAluno.setImmediate(true);
        cbxAluno.setContainerDataSource(dsAluno);
        cbxAluno.setItemCaptionPropertyId("nome");
        cbxAluno.setItemCaptionPropertyId("sobreNome");
        cbxAluno.setWidth("10cm");
        cbxAluno.addValueChangeListener(this);
        tabAluno.addComponent(cbxAluno);

//bean
@Entity
public class Aluno implements Serializable{ 
    private static final long serialVersionUID = 1L;

    @Id 
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long id;

    @NotNull @NotEmpty @Size(min=3, max=50) 
    private String nome;

    @NotNull @NotEmpty @Size(min=3, max=50)
    private String sobreNome;   

}

现在可以了

可以创建一个级联组合框,在其中选择Nome,并用“sobreNome”填充第二个组合框。或者,您必须将Nome和SobreName的所有值合并在一起。

我可能会说一些完全愚蠢的话,但是您是否尝试在类中实现了
toString
方法
Aluno

如果您可以使用自定义组合框,那么您可以这样做:

public class MyComboBox extends ComboBox {

    private static final long serialVersionUID = 1L;
    private List<String> myPropIds = Collections.emptyList();

    @Override
    public void setItemCaptionPropertyId(Object propId) {
        myPropIds = Arrays.asList(((String)propId).split(","));
    }

    @Override
    public String getItemCaption( Object itemId ) {
        StringBuilder sb = new StringBuilder();
        String delimiter = "";
        for (String propId : myPropIds) {
            Property<?> p = getContainerProperty(itemId, propId);
            sb.append(delimiter).append(getMyCaption(p));
            delimiter = " ";
        }
        return sb.toString();
    }

    private String getMyCaption(Property<?> p) {
        String caption = null;
        if (p != null) {
            Object value = p.getValue();
            if (value != null) {
                caption = value.toString();
            }
        }
        return caption != null ? caption : "";
    }

}

但是如果我做了
setItemCaptionPropertyId(“nome”+“+”SobreName)
就不起作用了。
public class MyComboBox extends ComboBox {

    private static final long serialVersionUID = 1L;
    private List<String> myPropIds = Collections.emptyList();

    @Override
    public void setItemCaptionPropertyId(Object propId) {
        myPropIds = Arrays.asList(((String)propId).split(","));
    }

    @Override
    public String getItemCaption( Object itemId ) {
        StringBuilder sb = new StringBuilder();
        String delimiter = "";
        for (String propId : myPropIds) {
            Property<?> p = getContainerProperty(itemId, propId);
            sb.append(delimiter).append(getMyCaption(p));
            delimiter = " ";
        }
        return sb.toString();
    }

    private String getMyCaption(Property<?> p) {
        String caption = null;
        if (p != null) {
            Object value = p.getValue();
            if (value != null) {
                caption = value.toString();
            }
        }
        return caption != null ? caption : "";
    }

}
ComboBox cb = new MyComboBox();
cb.setItemCaptionPropertyId("nome,sobreNome");
//...