`
f303153041
  • 浏览: 44868 次
社区版块
存档分类
最新评论

SWING 自动补全下拉框

 
阅读更多
reasons: 是从服务器一次拉取全部不重复的过往的填写记录
public class ComboxKeyAdapter extends KeyAdapter{
    public List<PriceChangeReason> reasons;
    public  ComboxKeyAdapter(List<PriceChangeReason> reasons){
    this.reasons = reasons;
    }
    @Override
public void keyReleased(KeyEvent e){
ComboBoxEditor editor = comboBox.getEditor();
            JTextField textField = (JTextField)editor.getEditorComponent();
            List<PriceChangeReason> result  =  new ArrayList<PriceChangeReason>();
            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();
  }
        }
    }
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics