3

I'm trying populate a combobox of vaadin7 with informations hashmap. I create a class that return a HashMap when I get the return I use a for each to populate this combobox but does show me only numbers and not the keys and values of hashmap.

I'm trying this.

/** states of brasil class */
public class EstadosBrasil {
private static final HashMap<String, String> uf = new HashMap();

/** return all states of brasil */
public static HashMap<String, String> getEstados(){             
    uf.put("AC", "AC");
    uf.put("AL", "AL");
    uf.put("AM", "AM");
    uf.put("AP", "AP");
    uf.put("BA", "BA");
    uf.put("CE", "CE");
    uf.put("DF", "DF");
    uf.put("ES", "ES");
    uf.put("FN", "FN");
    uf.put("GO", "GO");
    uf.put("MA", "MA");
    uf.put("MG", "MG");
    uf.put("MS", "MS");
    uf.put("MT", "MT");
    uf.put("PA", "PA");
    uf.put("PB", "PB");
    uf.put("PE", "PE");
    uf.put("PI", "PI");
    uf.put("PR", "PR");
    uf.put("RJ", "RJ");
    uf.put("RN", "RN");
    uf.put("RO", "RO");
    uf.put("RR", "RR");
    uf.put("RS", "RS");
    uf.put("SC", "SC");
    uf.put("SE", "SE");     
    uf.put("SP", "SP");
    uf.put("TO", "TO");

    return uf;
}   

}

// my combobox 
private ComboBox comboEstado;
comboEstado = new ComboBox("States");
comboEstado.setWidth("100px");
HashMap<String, String> estados = EstadosBrasil.getEstados();       
for(Entry<String, String> e : estados.entrySet()){                  
    Object obj = comboEstado.addItem();
    comboEstado.setItemCaption(e.getKey(), e.getValue());           
    comboEstado.setValue(obj);
}
mainLayout.addComponent(comboEstado);

Any idea ?

thanks

4

2 に答える 2

6

変化する-

Object obj = comboEstado.addItem();
comboEstado.setItemCaption(e.getKey(), e.getValue());           
comboEstado.setValue(obj);

に-

comboEstado.addItem(e.getKey());
comboEstado.setItemCaption(e.getKey(), e.getValue()); 

キーと値のペアの両方を表示したい場合は、次のようなことができます-

comboEstado.setItemCaption(e.getKey(), e.getKey() + " : " +  e.getValue());

ところで、あなたが価値観を変えようとしていることを願っています。キーと値の両方が同じ場合は、Setを使用できます。

于 2013-12-03T11:51:39.077 に答える
0

新しい Vaadin 8 API では、Combobox に addItems メソッドはありません。以下のコードは動作します:

Map<String, String> map = new HashMap<>();
ComboBox combobox = new ComboBox<>("My Combobox");
combobox.setItems(map);
combobox.setItemCaptionGenerator(new ItemCaptionGenerator() {
    @Override
    public String apply(Object o) {
        HashMap m = (HashMap) o;
        return m.keySet().stream().findFirst().get().toString();
    }
});
于 2018-11-29T14:52:30.447 に答える