I know that probably this question was repeat in this forum, but before I choose ask this question I was searching in internet by myself and almost my solution is done, but I have a wall in my face and I can't continue. Probably many people have the same problem when them want to program one autocompleter and this can be a solution for all.
My problem is . when I have the coincidences list , I don't know how I can update the DefaultComboBoxModel , because I'm trap in a infinity loop, because I'm using a documentListener.
Here is the code:
Main code:
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);
}
}
And this is the autocomplete 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;
}
}