Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

AutoComplete ComboBox

800313May 16 2009 — edited Aug 23 2010
Im trying to make an autocomplete combo box and i keep getting the following error, and I cant seen to find a way around it. Any help much appreciated


import javax.swing.event.*;
import javax.swing.*;
import java.util.*;

class TestAutoCompleteComboBox{
    private AutoCompleteComboBox acb;
     public TestAutoCompleteComboBox(){
         acb = new AutoCompleteComboBox(populateArray());
     }
     
     public ArrayList<String> populateArray(){
         ArrayList<String> array = new ArrayList<String>();
         array.add("n");
         array.add("new");
         array.add("news");
         array.add("newspaper");
         array.add("newspapers");
         array.add("naughty");
         array.add("newer");
         array.add("bad");
         array.add("baddy");
         return array;
     }
     public void makeUI(){
         
        JFrame.setDefaultLookAndFeelDecorated(true);
        JFrame frame = new JFrame("SuggestionComboBox Example");
        frame.add(acb);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	frame.setVisible(true);
    }
    public static void main(String[] args){
        new TestAutoCompleteComboBox().makeUI();
    }
}


public class AutoCompleteComboBox  extends JComboBox {
    private ArrayList<String> array;
    public AutoCompleteComboBox(ArrayList<String> array){
        super();
        this.array = array;
        this.setEditable(true);
        init();
    }
    
    public JComboBox getComboBox(){
        return this;
    }
    
    public void init(){
        if( !(this.getEditor().getEditorComponent() instanceof JTextField) ){
            System.err.println("ComboBox's Editor must be an instance of JTextField");
            return;
        }
        ((JTextField) this.getEditor().getEditorComponent()).getDocument().addDocumentListener(new DocumentListener(){
            JTextField textfield = (JTextField) getComboBox().getEditor().getEditorComponent();
            public void changedUpdate(DocumentEvent e) {
                findMatchingStrings(textfield.getText());
            }
            public void removeUpdate(DocumentEvent e) {
                //findMatchingStrings(textfield.getText());
            }
            public void insertUpdate(DocumentEvent e) {
                findMatchingStrings(textfield.getText());
            }
        });
    }
    
    public void findMatchingStrings(String entered){       
        ArrayList<String> autoCompleteArray = new ArrayList<String>();
        for(int i = 0;i<array.size();i++){
            if(array.get(i).startsWith(entered)){
                autoCompleteArray.add(array.get(i));
            } 
        }
        if(autoCompleteArray.size()>0){
            this.setModel(new DefaultComboBoxModel(autoCompleteArray.toArray()));
            this.showPopup();
        }

    }
}//end of class
Error
Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: Attempt to mutate in notification

at this line:  this.setModel(new DefaultComboBoxModel(autoCompleteArray.toArray()));
Thanks
Calypso
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 20 2010
Added on May 16 2009
11 comments
663 views