JComboBox doesn't allow to add or remove

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

JComboBox doesn't allow to add or remove

cr3ttzu
I have a program that takes the user input from a AutoComplete jComboBox and then send's the input to be stored into a text file.(AutoComplete is done using the library glazedlists_java15/1.8.0).

After using the Autocomplet feature I had to set the jComboBox to DefaultComboBoxModel.

When the user presses the Enter key, the jComboBox should update the list with the new Item typed from the keyboard, so the user can see the last typed item in the jComboBox list.

This is done by removing all the items from the jComboBox and then inserting them again from the text file.

The problem is that before having the AutoComplete feature I could just say jComboBox1.removeAllItems(); but now because of the model I have to do it with model.removeAllElements();

Here is the java class with an example:
aaaaaa.java


The problem is that model.removeAllElements(); and model.addElement(s); is not working so I can not update the jComboBox. Can you please take your time and help me find a solution. Thanks!
Reply | Threaded
Open this post in threaded view
|

Re: JComboBox doesn't allow to add or remove

James Lemieux
Hello,

The issue in play here is that AutoCompleteComboBoxModel (which is installed by AutoCompleteSupport) is not a MutableComboBoxModel. JComboBox.removeAllElements() requires this to be the case in order to work. While we can certainly improve this in an upcoming release (by making EventComboBoxModel, the parent class of AutoCompleteComboBoxModel implement MutableComboBoxModel), you can work around this limitation right now.

1. After installing AutoCompleteSupport, do NOT replace the ComboBoxModel with your own. Doing so will break autocompletion in certain use cases.

2. If you'd like to remove all elements from your combobox model, you can achieve this by removing all elements from the EventList that backs the model. Specifically, whatever EventList you passed into AutoCompleteSupport.install(...) you can simply call eventList.clear() to remove all items from the combobox.

Hope this helps you out,

James

On Mon, Nov 26, 2012 at 5:34 AM, cr3ttzu <[hidden email]> wrote:
I have a program that takes the user input from a AutoComplete jComboBox and
then send's the input to be stored into a text file.(AutoComplete is done
using the library glazedlists_java15/1.8.0).

After using the Autocomplet feature I had to set the jComboBox to
DefaultComboBoxModel.

When the user presses the Enter key, the jComboBox should update the list
with the new Item typed from the keyboard, so the user can see the last
typed item in the jComboBox list.

This is done by removing all the items from the jComboBox and then inserting
them again from the text file.

The problem is that before having the AutoComplete feature I could just say
jComboBox1.removeAllItems(); but now because of the model I have to do it
with model.removeAllElements();

Here is the java class with an example:
aaaaaa.java
<http://glazedlists.1045722.n5.nabble.com/file/n5709787/aaaaaa.java>


The problem is that model.removeAllElements(); and model.addElement(s); is
not working so I can not update the jComboBox. Can you please take your time
and help me find a solution. Thanks!



--
View this message in context: http://glazedlists.1045722.n5.nabble.com/JComboBox-doesn-t-allow-to-add-or-remove-tp5709787.html
Sent from the GlazedLists - User mailing list archive at Nabble.com.

Reply | Threaded
Open this post in threaded view
|

Re: JComboBox doesn't allow to add or remove

cr3ttzu
Hi!

I tried what you said but it does not remove any items from the jComboBox. Can you please take a look and let me know what I am doing wrong?

public class TestFrame {
       
        static JComboBox c= new JComboBox();
        private static final long serialVersionUID = 1L;
        private static JButton b = new JButton();
        static JFrame f = new JFrame();
        static String[] a = {"hi1" , "hi2", "hi3", "hi4","hi5"};
        /**
         * @param args
         */
        public static void TestFrame() {
                AutoCompleteSupport support = AutoCompleteSupport.install(c,
                                GlazedLists.eventListOf(a));
            JPanel test = new JPanel();
            test.add(b);
            test.add(c);
            c.addActionListener(new ActionListener()  
        {  
        @Override  
        public void actionPerformed(ActionEvent e)  
            {  
            c = (JComboBox)e.getSource();  

            if ( e.getActionCommand().equals("comboBoxEdited") )  
            GlazedLists.eventListOf(a).clear();

            }  
        });
            b.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                GlazedLists.eventListOf(a).clear();

                }  
            });
            f.add(test);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setVisible(true);
            f.setSize(500,500);

        }
        public static void main(String[] args) {
                javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                TestFrame();
            }
        });

        }
}

Thanks for your time and have a nice day!
Reply | Threaded
Open this post in threaded view
|

Re: JComboBox doesn't allow to add or remove

James Lemieux
So, you're not saving a reference to the EventList that backs your JComboBox. You're building a *new* EventList each time and then calling clear on that separate instance. The same exact instance of EventList that is passed into AutoCompleteSupport.install() method is the one that you will want to clear. You can see what I'm talking about in this modified version of your test application:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import ca.odell.glazedlists.EventList;
import ca.odell.glazedlists.GlazedLists;
import ca.odell.glazedlists.swing.AutoCompleteSupport;

public class TestFrame
{
    private static JComboBox c = new JComboBox();
    private static JButton b = new JButton();
    private static JFrame f = new JFrame();
    private static String[] a = {"hi1", "hi2", "hi3", "hi4", "hi5"};

    public static void TestFrame()
    {
        final EventList<String> items = GlazedLists.eventListOf(a);
        AutoCompleteSupport.install(c, items);
        JPanel test = new JPanel();
        test.add(b);
        test.add(c);
        c.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                c = (JComboBox) e.getSource();

                if (e.getActionCommand().equals("comboBoxEdited"))
                {
                    items.clear();
                }
            }
        });
        b.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                items.clear();
            }
        });
        f.add(test);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
        f.setSize(500, 500);
    }

    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                TestFrame();
            }
        });
    }
}


On Mon, Nov 26, 2012 at 10:38 PM, cr3ttzu <[hidden email]> wrote:
Hi!

I tried what you said but it does not remove any items from the jComboBox.
Can you please take a look and let me know what I am doing wrong?

public class TestFrame {

        static JComboBox c= new JComboBox();
        private static final long serialVersionUID = 1L;
        private static JButton b = new JButton();
        static JFrame f = new JFrame();
        static String[] a = {"hi1" , "hi2", "hi3", "hi4","hi5"};
        /**
         * @param args
         */
        public static void TestFrame() {
                AutoCompleteSupport support = AutoCompleteSupport.install(c,
                                GlazedLists.eventListOf(a));
            JPanel test = new JPanel();
            test.add(b);
            test.add(c);
            c.addActionListener(new ActionListener()
        {
        @Override
        public void actionPerformed(ActionEvent e)
            {
            c = (JComboBox)e.getSource();

            if ( e.getActionCommand().equals("comboBoxEdited") )
                GlazedLists.eventListOf(a).clear();

            }
        });
            b.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                        GlazedLists.eventListOf(a).clear();

                }
            });
            f.add(test);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setVisible(true);
            f.setSize(500,500);

        }
        public static void main(String[] args) {
                javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                TestFrame();
            }
        });

        }
}

Thanks for your time and have a nice day!



--
View this message in context: http://glazedlists.1045722.n5.nabble.com/JComboBox-doesn-t-allow-to-add-or-remove-tp5709787p5709789.html
Sent from the GlazedLists - User mailing list archive at Nabble.com.