美文网首页
Java Swing 自动补全下拉框

Java Swing 自动补全下拉框

作者: 秋兰兮青青 | 来源:发表于2017-05-08 14:33 被阅读0次

    reasons: 是从服务器一次拉取全部不重复的过往的填写记录

    public class ComboxKeyAdapter extends KeyAdapter{

    public List reasons;

    public  ComboxKeyAdapter(List reasons){

    this.reasons = reasons;

    }

    @Override

    public void keyReleased(KeyEvent e){

    ComboBoxEditor editor = comboBox.getEditor();

    JTextField textField = (JTextField)editor.getEditorComponent();

    List result  =  new ArrayList();

    String objStr = textField.getText();

    if(objStr!="" && !objStr.equals("")){

    for(PriceChangeReason res : reasons){

    if(objStr.equals(res.getMemo())

    || res.getMemo().contains(objStr)

    || res.getMemo().startsWith(objStr)

    || res.getMemo().endsWith(objStr)){

    result.add(res);

    }

    }

    String[] memos;

    if(result.size() > 0){

    comboBox.hidePopup();

    comboBox.removeAllItems();

    memos = new String[result.size()];

    for(int i = 0; i < result.size();i++){

    PriceChangeReason reason  = result.get(i);

    memos[i] = reason.getMemo();

    comboBox.addItem(reason.getMemo());

    }

    comboBox.setSelectedItem(objStr);

    comboBox.showPopup();

    }

    }else{

    comboBox.hidePopup();

    comboBox.removeAllItems();

    for(int i = 0; i < reasons.size();i++){

    PriceChangeReason reason  = reasons.get(i);

    comboBox.addItem(reason.getMemo());

    }

    comboBox.setSelectedItem("");

    comboBox.showPopup();

    }

    }

    }

    相关文章

      网友评论

          本文标题:Java Swing 自动补全下拉框

          本文链接:https://www.haomeiwen.com/subject/vmimtxtx.html