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,993評論 19 139
  • 背景 一年多以前我在知乎上答了有關LeetCode的問題, 分享了一些自己做題目的經驗。 張土汪:刷leetcod...
    土汪閱讀 12,779評論 0 33
  • 1. Java基礎部分 基礎部分的順序:基本語法,類相關的語法,內部類的語法,繼承相關的語法,異常的語法,線程的語...
    子非魚_t_閱讀 31,779評論 18 399
  • Java經典問題算法大全 /*【程序1】 題目:古典問題:有一對兔子,從出生后第3個月起每個月都生一對兔子,小兔子...
    趙宇_阿特奇閱讀 1,916評論 0 2
  • Correctness AdapterViewChildren Summary: AdapterViews can...
    MarcusMa閱讀 8,926評論 0 6