我寫了一個自動完成組合框程序,其中我搜索用戶在文件中輸入的單詞。該程序工作正常,但是,combobox editor
不會返回任何東西時,鍵入內容。我不知道這是爲什麼..這是處理問題的代碼塊。jComboBox編輯器返回空字符串
// in GUI class constructor
InstantSearchBox = new JComboBox();
InstantSearchBox.setEditable(true);
/*****/
KeyHandler handle = new KeyHandler();
InstantSearchBox.getEditor().getEditorComponent().addKeyListener(handle);
// Keylistener class (KeyPressed method)
try
{
dataTobeSearched = InstantSearchBox.getEditor().getItem().toString();
// the string variable is empty for some reason
System.out.println ("Data to be searched " + dataTobeSearched);
}
catch (NullPointerException e)
{
e.printStackTrace();
}
問候
我會懷疑這種方式可能會提出一些建議......,爲了更好的幫助,儘快發佈一個[SSCCE](http://sscce.org/),短的可運行的,可編譯的,只是關於'JFrame ''JComboBox'和硬編碼值'Items' – mKorbel
這裏有兩個答案,不是,從來沒有,這種方式不可能在'Editor'中捕獲'non_finalized KeyEvents' vs在Swing – mKorbel