Quantcast

Problems useing autocomplete on a dynamic filled ComboBox

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Problems useing autocomplete on a dynamic filled ComboBox

GGK_01
Hi,

I have a problem while deleting and then filling my comboBox dynamicly.
After filling it and work perfect with AutoComplete,  I clear the EventList an adding new Itmes to the Comobox I got the error:
"Cannot use this method with a non-Mutable data model."

Here a quick and dirty typed Code...
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.EventQueue;
 
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
 
import javax.swing.JComboBox;
 
import ca.odell.glazedlists.EventList;
import ca.odell.glazedlists.GlazedLists;
import ca.odell.glazedlists.swing.AutoCompleteSupport;
 
import com.sun.java.swing.plaf.nimbus.ComboBoxComboBoxArrowButtonPainter;
 
public class AutoComplete extends JFrame {
 
    private JPanel contentPane;
    private JButton btnNewButton;
    private static JComboBox comboBox;
    private static EventList<String> items;
   
    private static ArrayList<String> obj;
    private JButton btnNewButton_1;
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    AutoComplete frame = new AutoComplete();
                    frame.setVisible(true);
                   
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
 
   
    public static void fillArray() {
        obj = new ArrayList<String> ();
        for (int i = 1;i<6;i++) {
            obj.add("hi"+i);
           
        }
        System.out.println("Array: "+obj);
        fillCombo();
       
    }
   
    public static void fillArray2() {
        obj = new ArrayList<String> ();
        for (int i = 100;i<106;i++) {
            obj.add("hi"+i);
           
        }
        System.out.println("Array: "+obj);
        items = GlazedLists.eventList(obj);
       
       
        System.out.println("items: "+items);
       
        comboBox.addItem(obj);
       
    }
   
    public static void fillCombo() {
        items = GlazedLists.eventList(obj);
        System.out.println("items: "+items);
        AutoCompleteSupport.install(comboBox, items);
    }
   
   
   
    /**
     * Create the frame.
     */
    public AutoComplete() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 102);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(new BorderLayout(0, 0));
   
        btnNewButton = new JButton("Fill Box");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                fillArray();
            }
        });
        contentPane.add(btnNewButton, BorderLayout.NORTH);
       
        JButton btnBeenden = new JButton("Beenden");
        btnBeenden.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                System.exit(0);
            }
        });
        contentPane.add(btnBeenden, BorderLayout.EAST);
       
        comboBox = new JComboBox();
       
        contentPane.add(comboBox, BorderLayout.CENTER);
       
        btnNewButton_1 = new JButton("Create New List");
        btnNewButton_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                items.clear();
                fillArray2();
           
            }
        });
        contentPane.add(btnNewButton_1, BorderLayout.WEST);
    }
   
 
}


Would be great, when someone give me a solution for my problem

Thanks

GGK

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Problems useing autocomplete on a dynamic filled ComboBox

James Lemieux
Do not attempt to add new items through the ComboBoxModel (it is not mutable, as the message is telling you). Instead, add them to the EventList.

i.e. change fillArray2() to this:

    public static void fillArray2() { 
        for (int i = 100;i<106;i++) { 
            items.add("hi"+i); // notice we're adding the new item to the EventList here, not the ComboBoxModel
        } 
    }


On Mon, May 12, 2014 at 6:31 AM, g3949 <[hidden email]> wrote:
Hi,

I have a problem while deleting and then filling my comboBox dynamicly.
After filling it and work perfect with AutoComplete,  I clear the EventList an adding new Itmes to the Comobox I got the error:
"Cannot use this method with a non-Mutable data model."

Here a quick and dirty typed Code...
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.EventQueue;
 
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
 
import javax.swing.JComboBox;
 
import ca.odell.glazedlists.EventList;
import ca.odell.glazedlists.GlazedLists;
import ca.odell.glazedlists.swing.AutoCompleteSupport;
 
import com.sun.java.swing.plaf.nimbus.ComboBoxComboBoxArrowButtonPainter;
 
public class AutoComplete extends JFrame {
 
    private JPanel contentPane;
    private JButton btnNewButton;
    private static JComboBox comboBox;
    private static EventList<String> items;
   
    private static ArrayList<String> obj;
    private JButton btnNewButton_1;
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    AutoComplete frame = new AutoComplete();
                    frame.setVisible(true);
                   
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
 
   
    public static void fillArray() {
        obj = new ArrayList<String> ();
        for (int i = 1;i<6;i++) {
            obj.add("hi"+i);
           
        }
        System.out.println("Array: "+obj);
        fillCombo();
       
    }
   
    public static void fillArray2() {
        obj = new ArrayList<String> ();
        for (int i = 100;i<106;i++) {
            obj.add("hi"+i);
           
        }
        System.out.println("Array: "+obj);
        items = GlazedLists.eventList(obj);
       
       
        System.out.println("items: "+items);
       
        comboBox.addItem(obj);
       
    }
   
    public static void fillCombo() {
        items = GlazedLists.eventList(obj);
        System.out.println("items: "+items);
        AutoCompleteSupport.install(comboBox, items);
    }
   
   
   
    /**
     * Create the frame.
     */
    public AutoComplete() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 102);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(new BorderLayout(0, 0));
   
        btnNewButton = new JButton("Fill Box");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                fillArray();
            }
        });
        contentPane.add(btnNewButton, BorderLayout.NORTH);
       
        JButton btnBeenden = new JButton("Beenden");
        btnBeenden.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                System.exit(0);
            }
        });
        contentPane.add(btnBeenden, BorderLayout.EAST);
       
        comboBox = new JComboBox();
       
        contentPane.add(comboBox, BorderLayout.CENTER);
       
        btnNewButton_1 = new JButton("Create New List");
        btnNewButton_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                items.clear();
                fillArray2();
           
            }
        });
        contentPane.add(btnNewButton_1, BorderLayout.WEST);
    }
   
 
}


Would be great, when someone give me a solution for my problem

Thanks

GGK


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Problems useing autocomplete on a dynamic filled ComboBox

GGK_01
This post has NOT been accepted by the mailing list yet.
In reply to this post by GGK_01
Thanks...exact that was the problem...

Solved Thanks!

GGK
Loading...