Display two or more properties in a ComboBox?
I am trying to display two properties in a ComboBox. I tried setItemCaptionPropertyId()
but this only displays nome
and I want to display nome
and sobreNome
or more properties.
I am trying to do 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 do it?
** I solved the problem **
I created a new attribute named caption
and concatenated strings that I want. After creating getCaption () that return the caption value,
Decision.
@Transient
private String caption;
public String getCaption(){
caption = nome + " " + sobreNome;
return caption;
}
cbxAluno.setItemCaptionPropertyId("caption");
it works now.!
I could say something completely stupid, but have you tried to implement the method toString
in your class Aluno
?
You can create a cascading combo box where you select "Nome" and it populates the second combo box "sobreNome". Or you will need to concatenate all the Nome and SobreNome values together.
If you can use a custom ComboBox, you can 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 : "";
}
}
Using:
ComboBox cb = new MyComboBox();
cb.setItemCaptionPropertyId("nome,sobreNome");
//...