FernandoPaiva
FernandoPaiva

Reputation: 4460

Display two or more properties at ComboBox?

I'm trying display two properties at a ComboBox. I tried setItemCaptionPropertyId() but this displays only nome and I want to display nome and sobreNome or more properties.

I'm trying this.

//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;   

}

How to I do that ?

** I Solved the problem **

I created a new attribute with name caption, and concatenated Strings that I want. After create a getCaption() that return caption value;

The solution.

@Transient
private String caption;

public String getCaption(){
   caption = nome + " " + sobreNome;
   return caption;
}

cbxAluno.setItemCaptionPropertyId("caption");

now works.!

Upvotes: 3

Views: 202

Answers (3)

Krayo
Krayo

Reputation: 2510

If you can use custom ComboBox then you could do this:

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 : "";
    }

}

Usage:

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

Upvotes: 0

R2B2
R2B2

Reputation: 1591

I might say something completely stupid, but did you try to implement the method toString in your class Aluno?

Upvotes: 2

Stephen McConnell
Stephen McConnell

Reputation: 64

One might create a cascading Combo Box in which you select the Nome and it populates a second combo box with the "sobreNome". Or you will have to concatonate all the values of Nome and SobreNome together.

Upvotes: 0

Related Questions