我写了一个自动完成组合框程序,其中我搜索用户在文件中输入的单词。该程序工作正常,但是,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