我必须在搜索组合框中创建自动完成功能。为此我创建了可编辑的组合框并添加了文档监听器。每当有人输入任何内容时,我都会向solr服务器发送请求,并使用来自solr服务器的响应更新组合框列表。我的问题是我想用新的响应刷新组合框,因为我从组合框中删除所有项目并添加新的响应,但它不起作用。示例代码如下
SearchComboField = new JComboBox();
final DefaultComboBoxModel model = new DefaultComboBoxModel();
SearchComboField.setEditable(true);
JTextComponent editorComponent = (JTextComponent) SearchComboField.getEditor().getEditorComponent();
Document document = editorComponent.getDocument();
document.addDocumentListener(new DocumentListener(){
@Override
public void insertUpdate(DocumentEvent e) {
System.out.println("insertUpdate");
}
@Override
public void removeUpdate(DocumentEvent e) {
update();
}
@Override
public void changedUpdate(DocumentEvent e) {
System.out.println("changedUpdate");
}
public void update() {
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run() {
model.removeAllElements();
//I will add Server response here
model.addElement("One");
}
});
}
});
AutoCompleteDecorator.decorate(SearchComboField);
答案 0 :(得分:0)
试试这个:
final DefaultComboBoxModel model = new DefaultComboBoxModel();
JComboBox SearchComboField = new JComboBox(model);