Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/378.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_Combobox_Zk - Fatal编程技术网

Java 按代码设置组合框中的选定项

Java 按代码设置组合框中的选定项,java,combobox,zk,Java,Combobox,Zk,如果值列表只有一项,我想通过代码选择组合框的第一项。我试过这个: Comboitem item = new Comboitem(); for(Empresa e : empresasList){ item.setValue(e.getEmpId()); item.setLabel(e.getEmpNombre()); item.setParent(cb_empresa); } if(empresasList.size()==1){

如果值列表只有一项,我想通过代码选择组合框的第一项。我试过这个:

Comboitem item = new Comboitem();
for(Empresa e : empresasList){              
    item.setValue(e.getEmpId());
    item.setLabel(e.getEmpNombre());
    item.setParent(cb_empresa);
}
if(empresasList.size()==1){             
    idEmpresa = empresasList.get(0).getEmpId();
//cb_empresa.setSelectedIndex(0);
    cb_empresa.setSelectedItem(item);
}
但它不起作用。我还尝试了注释行:

//cb_empresa.setSelectedIndex(0);
有什么帮助吗


谢谢

我不知道
item.setParent(cb_empresa)对视图具有即时效果。

您可以稍后尝试设置索引,或者在刷新cb_empresa之后。

首先,您的代码有缺陷。您必须创建与empresasList.size()一样多的Comboitems。我猜是这样的

for(Empresa e : empresasList){
    Comboitem item = new Comboitem();              
    item.setValue(e.getEmpId());
    item.setLabel(e.getEmpNombre());
    cb_empresa.appendItem(item);
}
if(cb_empresa.getItemCount()==1){             
    cb_empresa.setSelectedIndex(0);
}
应该有用。但是,正如前面提到的,ZK中有一个错误,setSelectedIndex()必须推迟。就我所记得的,这个错误已经被修复了。如果没有,则可以使用Event.echoovent()作为解决方法

for(Empresa e : empresasList){
    Comboitem item = new Comboitem();              
    item.setValue(e.getEmpId());
    item.setLabel(e.getEmpNombre());
    cb_empresa.appendChild(item);
}
if(cb_empresa.getItemCount()==1){             
    cb_empresa.setSelectedIndex(0);
}