Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.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 用HashMap填充组合框?_Java_Frameworks_Vaadin_Vaadin7 - Fatal编程技术网

Java 用HashMap填充组合框?

Java 用HashMap填充组合框?,java,frameworks,vaadin,vaadin7,Java,Frameworks,Vaadin,Vaadin7,我正在尝试用信息hashmap填充vaadin7的组合框。我创建了一个类,当我得到返回值时返回一个HashMap,我使用for each来填充这个组合框,但只显示数字,而不显示HashMap的键和值 我正在试这个 /** states of brasil class */ public class EstadosBrasil { private static final HashMap<String, String> uf = new HashMap(); /** return a

我正在尝试用信息hashmap填充vaadin7的组合框。我创建了一个类,当我得到返回值时返回一个HashMap,我使用for each来填充这个组合框,但只显示数字,而不显示HashMap的键和值

我正在试这个

/** 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;
}   
/**巴西类州*/
巴西公共级EstadosBrasil{
私有静态final HashMap uf=new HashMap();
/**返回巴西所有州*/
公共静态HashMap 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”);
返回uf;
}   
}

//我的组合框
私人组合框comboEstado;
comboEstado=新组合框(“状态”);
comboEstado.setWidth(“100px”);
HashMap estados=EstadosBrasil.getEstados();
对于(条目e:estados.entrySet()){
Object obj=comboEstado.addItem();
setItemCaption(e.getKey(),e.getValue());
comboEstado.setValue(obj);
}
mainLayout.addComponent(comboEstado);
有什么想法吗

谢谢你的改变-

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

如果希望同时显示键和值对,可以执行类似的操作-

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

顺便说一下,我希望你能改变这些值。如果键和值都相同,您可以只使用一个集合。

在新的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();
    }
});
Map Map=newhashmap();
ComboBox ComboBox=新的ComboBox(“我的ComboBox”);
combobox.setItems(映射);
combobox.setItemCaptionGenerator(新的ItemCaptionGenerator(){
@凌驾
公共字符串应用(对象o){
HashMap m=(HashMap)o;
返回m.keySet().stream().findFirst().get().toString();
}
});

注意:为什么不使用哈希集?键/值对总是相同的。@FernandoPaiva很高兴听到。:)
ComboBox
没有
addItem
方法,首先,我需要使用
getItems()
,然后在版本8上使用
add()
addAll()
@OrdinaryDraft,您可以调用add。
comboEstado.setItemCaption(e.getKey(), e.getKey() + " : " +  e.getValue());
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();
    }
});