Java Swing 自动补全下拉框

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();

}

}

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,948评论 18 139
  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 12,771评论 0 33
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,766评论 18 399
  • Java经典问题算法大全 /*【程序1】 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子...
    赵宇_阿特奇阅读 1,910评论 0 2
  • Correctness AdapterViewChildren Summary: AdapterViews can...
    MarcusMa阅读 8,925评论 0 6