我编写了一个自动完成组合框程序,在该程序中搜索用户在文件中输入的单词。该程序工作正常,但是,在combobox editor
其中输入内容时不会返回任何内容。我不知道为什么会这样。这是处理该问题的代码块。
// 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 ();
}
问候