我知道可能这个问题在这个论坛上重复了,但在我选择问这个问题之前,我一直在互联网上搜索,几乎我的解决方案已经完成,但我的墙上有一堵墙,我无法继续。可能很多人在想要编写一个自动完成程序时遇到同样的问题,这可以成为所有人的解决方案。
我的问题是。当我有巧合列表时,我不知道如何更新DefaultComboBoxModel,因为我陷入了无限循环,因为我正在使用documentListener。
以下是代码:
主要代码:
public class MainFrame extends JFrame{
public MainFrame(){
initFrame();
}
private void initFrame() {
setTitle("Auto Completer");
setSize(284 , 70 );;
setLocationRelativeTo(null);;
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
getContentPane().setLayout(new BorderLayout(0, 0));
GComboBox comboBox = new GComboBox();
comboBox.getData().add("George");
comboBox.getData().add("Michael");
comboBox.getData().add("Mike");
comboBox.getData().add("Tom");
comboBox.getData().add("Elvis");
comboBox.commitData();
getContentPane().add(comboBox, BorderLayout.NORTH);
}
public static void main(String[] args) {
new MainFrame().setVisible(true);
}
}
这是自动完成JComboBox
public class GComboBox extends JComboBox<String> {
private List<String> data = new ArrayList<String>();
private DefaultComboBoxModel<String> comboModel;
private JTextComponent editTextField;
private boolean loadFinished = false;
public GComboBox(){
setEditable(true);
initComponent();
}
public void commitData(){
Vector<String> vector = new Vector<String>();
for (int i = 0 ; i < data.size() ; i++){
vector.add(data.get(i));
}
comboModel = new DefaultComboBoxModel<String>(vector);
this.setModel(comboModel);
editTextField.setText("");
loadFinished = true;
}
private void initComponent() {
createDocumentListeners();
}
private void createDocumentListeners() {
editTextField = (JTextComponent)getEditor().getEditorComponent();
editTextField.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void changedUpdate(DocumentEvent e) {}
@Override
public void insertUpdate(DocumentEvent e) {
update();
}
@Override
public void removeUpdate(DocumentEvent e) {
update();
}
public void update(){
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run() {
List<String> founds = getCoincidences(editTextField.getText());
//if all words was loaded in the combo
if ( loadFinished ){
setCoincidencesInModel(founds);
setPopupVisible(true);
System.out.println("Load finished");
}
//Here I can see the coincidences
for ( String itemFound : founds){
System.out.println("Words with coincidences -> " + itemFound);
}
}
});
}
});
}
public List<String> getCoincidences(String wordEntered){
List <String> newList = new ArrayList<String>();
for (int i = 0 ; i < data.size() ; i++){
if( data.get(i).contains(wordEntered)){
newList.add(data.get(i));
}
}
return newList;
}
public void setCoincidencesInModel(List<String> coincidenceList){
comboModel.removeAllElements();
Vector<String> vector = new Vector<String>();
for ( String coincidence : coincidenceList ){
comboModel.addElement(coincidence);
vector.add(coincidence);
}
DefaultComboBoxModel<String> newModel = new DefaultComboBoxModel<String>(vector);
setModel(newModel);
}
public List<String> getData() {
return data;
}
public void setData(List<String> data) {
this.data = data;
}
}