zoukankan      html  css  js  c++  java
  • 下拉框左编码右名称获取数据

    //获取数据

    List<Declarant> list = this.decParamLogic.findDeclarants(request);

    //ItemProperty是code name 类
    List<ItemProperty> propertys = new ArrayList();
    if (list != null) {
    for (Declarant declarant : list) {
    propertys.add(new ItemProperty(declarant.getCode(), declarant.getName()));
    }
    }

    //设置成下拉框样式
    tfDeclarant.setModel(new DefaultComboBoxModel(propertys.toArray()));

    //值转换器方法,解决字符串保存问题

    this.tfDeclarant.setConvert(new ItemPropertyValueConvert(new DefaultComboBoxModel(propertys.toArray())));

    import java.io.Serializable;
    import java.lang.reflect.InvocationTargetException;

    import javax.swing.JComboBox;

    import org.apache.commons.beanutils.PropertyUtils;

    /**
    * @author Administrator
    *
    * // change the template for this generated type comment go to Window - Preferences - Java - Code Style - Code Templates
    */
    public class ItemProperty implements Serializable{

    private String code;
    private String name;

    @Override
    public boolean equals(Object o) {
    if (o == null) {
    return false;
    }
    if (o == this) {
    return true;
    }
    if (!(o instanceof ItemProperty)) {
    return false;
    }
    ItemProperty tmp = (ItemProperty) o;
    if (tmp.code != null && tmp.code.equals(code)
    && tmp.name != null && tmp.name.equals(name)) {
    return true;
    }
    return false;
    }

    public ItemProperty() {
    }

    public ItemProperty(String code, String name) {
    this.code = code;
    this.name = name;
    }

    public String toString() {
    return name;
    }

    public String getCode() {
    return code;
    }

    public void setCode(String code) {
    this.code = code;
    }

    public String getName() {
    return name;
    }

    public void setName(String name) {
    this.name = name;
    }

    public static int getIndexByCode(String code, JComboBox ccb) {
    if (ccb == null || code == null || code.equals("")
    || ccb.getItemCount() <= 0) {
    return -1;
    }
    if (ccb.getItemAt(0) instanceof ItemProperty) {
    for (int i = 0; i < ccb.getItemCount(); i++) {
    if (((ItemProperty) ccb.getItemAt(i)).getCode().equals(code)) {
    return i;
    }
    }
    }
    return -1;
    }

    public static ItemProperty getItemProperty(String code, JComboBox ccb) {
    if (ccb == null || code == null || code.equals("")
    || ccb.getItemCount() <= 0) {
    return null;
    }
    if (ccb.getItemAt(0) instanceof ItemProperty) {
    for (int i = 0; i < ccb.getItemCount(); i++) {
    ItemProperty item = (ItemProperty) ccb.getItemAt(i);
    if (item.getCode().equals(code)) {
    return item;
    }
    }
    }
    return null;
    }

    public static int getIndexByName(String name, JComboBox ccb) {
    if (ccb == null || name.equals("") || ccb.getItemCount() <= 0) {
    return -1;
    }
    if (ccb.getItemAt(0) instanceof ItemProperty) {
    for (int i = 0; i < ccb.getItemCount(); i++) {
    if (((ItemProperty) ccb.getItemAt(i)).getName().equals(name)) {
    return i;
    }
    }
    }
    return -1;
    }

    public static int getIndexByValue(String fieldName, Object value,
    JComboBox ccb) {
    if (ccb == null || value == null || value.equals("")
    || ccb.getItemCount() <= 0) {
    return -1;
    }
    for (int i = 0; i < ccb.getItemCount(); i++) {
    Object obj = ccb.getItemAt(i);
    Object currValue = null;
    try {
    currValue = PropertyUtils.getNestedProperty(obj, fieldName);
    } catch (IllegalAccessException e) {
    e.printStackTrace();
    } catch (InvocationTargetException e) {
    e.printStackTrace();
    } catch (NoSuchMethodException e) {
    e.printStackTrace();
    }
    if (value.equals(currValue)) {
    return i;
    }
    }
    return -1;
    }

    public static String getItemPropertyValue(JComboBox cbb) {
    ItemProperty item = (ItemProperty) (cbb.getSelectedIndex() > -1 ? cbb
    .getSelectedItem() : null);
    if (item != null) {
    return item.getCode();
    } else {
    return null;
    }
    }
    }

  • 相关阅读:
    SQL Server 连接字符串和身份验证 学习
    何時需要重启 OFBiz
    开源软件文档网址
    OFBIZ 10.04 开发环境搭建(ofbiz+mysql+eclipse)
    ofbiz 之minilang解析
    ofbiz之entity实体写法
    ofbiz多表外键关联查询
    ofbiz学习地址
    配置文件中的mime-mapping元素(ofbiz/framework/catalina/config/mime-type)(
    SQL连接 自我学习,跑完秒懂
  • 原文地址:https://www.cnblogs.com/tiansan/p/7324454.html
Copyright © 2011-2022 走看看