欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

ZK 根据传入的值设置listbox的选中项

程序员文章站 2022-04-05 15:19:48
...
public static void setSelecteIndexByLabel(Listbox box, String label, Class cl, String varName){
List<Listitem> list = box.getItems();
if(list != null){
int index = 0;
for(int i = 0; i < list.size(); i++){
Listitem item = list.get(i);
if(item == null)continue;
if(item.getLabel().equals(label)){
box.setSelectedIndex(i);
index = i;
break;
}
}
if(index == 0){
try {
varName = StringUtils.upperCase(varName.substring(0, 1))+varName.substring(1);
Method m = cl.getDeclaredMethod("get"+varName, null);
ListModel model = box.getModel();
for(int i = 0; i < list.size(); i++){
Object o = model.getElementAt(i);
if(o == null) continue;
String val = String.valueOf(m.invoke(o, new Object[0]));
if(label.equals(val)){
box.setSelectedIndex(i);
break;
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}
}
相关标签: zk