We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,688 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Updating Jcombobox on runtime.

I need to update the items of JCombobox from the database during runtime. I had made the JCombobox editable and what i need to do is type something in the editable combobox and search the database and only display the items matching the text. For example there are if i type "ap" in the editbox of JCombobox then i want only the items containing "ap" like "apes" and "apple" i can have them in the console during runtime but when i try to set those item as an items of Jcombobox i am getting error something including "MUtation....". how can i do this . Thanx in advance

4
Contributors
17
Replies
5 Days
Discussion Span
1 Year Ago
Last Updated
18
Views
47pirates
Junior Poster
129 posts since Dec 2009
Reputation Points: 19
Solved Threads: 1
Skill Endorsements: 0

Why don't you post the entire error message? And some code? You generally do it like this:

cmb.removeAllItems();
cmb.addItem("item 1");
cmb.addItem("item 2");
...

If there's a change of having duplicate strings, create a class and override the toString() method, or use anonymous classes, like:

cmb.removeAllItems();
cmb.addItem(new Object() { public String toString() { return "item 1"; } });
cmb.addItem(new Object() { public String toString() { return "item 2"; } });
...
nmaillet
Posting Pro
542 posts since Aug 2008
Reputation Points: 111
Solved Threads: 104
Skill Endorsements: 4

SEARCH:chicke
SEARCHED ITEM:chicken
Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: Attempt to mutate in notification

 at javax.swing.text.AbstractDocument.writeLock(AbstractDocument.java:1323)
        at javax.swing.text.AbstractDocument.replace(AbstractDocument.java:644)
        at javax.swing.text.JTextComponent.setText(JTextComponent.java:1693)
        at javax.swing.plaf.metal.MetalComboBoxEditor$1.setText(MetalComboBoxEditor.java:44)
        at javax.swing.plaf.basic.BasicComboBoxEditor.setItem(BasicComboBoxEditor.java:60)
        at javax.swing.JComboBox.configureEditor(JComboBox.java:1383)
        at javax.swing.plaf.basic.BasicComboBoxUI$Handler.contentsChanged(BasicComboBoxUI.java:1817)
        at javax.swing.AbstractListModel.fireContentsChanged(AbstractListModel.java:100)
        at javax.swing.DefaultComboBoxModel.setSelectedItem(DefaultComboBoxModel.java:88)
        at javax.swing.DefaultComboBoxModel.removeElementAt(DefaultComboBoxModel.java:140)
        at javax.swing.JComboBox.removeItemAt(JComboBox.java:739)
        at ui.restuarant.ExternalOrderPanel$1.removeUpdate(ExternalOrderPanel.java:64)
        at javax.swing.text.AbstractDocument.fireRemoveUpdate(AbstractDocument.java:243)
        at javax.swing.text.AbstractDocument.handleRemove(AbstractDocument.java:608)
        at javax.swing.text.AbstractDocument.remove(AbstractDocument.java:576)
        at javax.swing.text.DefaultEditorKit$DeletePrevCharAction.actionPerformed(DefaultEditorKit.java:1045)
        at javax.swing.SwingUtilities.notifyAction(SwingUtilities.java:1633)
        at javax.swing.JComponent.processKeyBinding(JComponent.java:2839)
        at javax.swing.JComponent.processKeyBindings(JComponent.java:2874)
        at javax.swing.JComponent.processKeyEvent(JComponent.java:2802)
        at java.awt.Component.processEvent(Component.java:6040)
        at java.awt.Container.processEvent(Container.java:2041)
        at java.awt.Component.dispatchEventImpl(Component.java:4630)
        at java.awt.Container.dispatchEventImpl(Container.java:2099)
        at java.awt.Component.dispatchEvent(Component.java:4460)
        at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1850)
        at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:712)
        at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:990)
        at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:855)
        at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:676)
        at java.awt.Component.dispatchEventImpl(Component.java:4502)
        at java.awt.Container.dispatchEventImpl(Container.java:2099)
        at java.awt.Window.dispatchEventImpl(Window.java:2475)
        at java.awt.Component.dispatchEvent(Component.java:4460)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)*Emphasized Text Here*
47pirates
Junior Poster
129 posts since Dec 2009
Reputation Points: 19
Solved Threads: 1
Skill Endorsements: 0

This is the error i get when i try to change the items during runtime plzzz help

47pirates
Junior Poster
129 posts since Dec 2009
Reputation Points: 19
Solved Threads: 1
Skill Endorsements: 0

can you show the code where that exception is thrown?

stultuske
Industrious Poster
4,489 posts since Jan 2007
Reputation Points: 1,377
Solved Threads: 627
Skill Endorsements: 25
 final JTextComponent tc = (JTextComponent) itemNameSelector.getEditor().getEditorComponent();
        tc.getDocument().addDocumentListener(new DocumentListener() {

            public void insertUpdate(DocumentEvent e) {
                try {
                    searchingText = e.getDocument().getText(0, e.getDocument().getLength());
                    System.out.println("SEARCH:" + searchingText);
                    rest.searchMenuItem(searchingText);
                    itemNameSelector.addItem(searchingText);
                    itemNameSelector.revalidate();

                } catch (BadLocationException ex) {
                    Logger.getLogger(ExternalOrderPanel.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

            public void removeUpdate(DocumentEvent e) {
                try {
                    searchingText = e.getDocument().getText(0, e.getDocument().getLength());
                    System.out.println("SEARCH:" + searchingText);
                    rest.searchMenuItem(searchingText);
                    itemNameSelector.removeItemAt(0);
                    itemNameSelector.revalidate();


                } catch (BadLocationException ex) {
                    Logger.getLogger(ExternalOrderPanel.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

Here searchingText is a String that is typed in Editable Combobox and according to that String i need to update the items of that combobox. But when i try this code i get the above error. But i can get the desired item names on my console.

47pirates
Junior Poster
129 posts since Dec 2009
Reputation Points: 19
Solved Threads: 1
Skill Endorsements: 0

The problem seems to be that the document is locked when the listener is called, so when you try to update the document you can't get the lock to write to it. The solution, if you want to keep it working the same way, is to use SwingUtilities.invokeLater to run the update after your listener has finished.

JamesCherrill
... trying to help
Moderator
8,666 posts since Apr 2008
Reputation Points: 2,636
Solved Threads: 1,476
Skill Endorsements: 33

You want to have a look at this thread. I don't have time to look at the solutions now, but they may be of some help.

nmaillet
Posting Pro
542 posts since Aug 2008
Reputation Points: 111
Solved Threads: 104
Skill Endorsements: 4

will u please show me where can i call that SwingUtilities.invokeLater method i have done like this but its not working.

 final JTextComponent tc = (JTextComponent) itemNameSelector.getEditor().getEditorComponent();
        tc.getDocument().addDocumentListener(new DocumentListener() {

            public void insertUpdate(DocumentEvent e) {
                try {
                    searchingText = e.getDocument().getText(0, e.getDocument().getLength());
                    System.out.println("SEARCH:" + searchingText);

                } catch (BadLocationException ex) {
                    Logger.getLogger(ExternalOrderPanel.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

            public void removeUpdate(DocumentEvent e) {
                try {
                    searchingText = e.getDocument().getText(0, e.getDocument().getLength());
                    System.out.println("SEARCH:" + searchingText);
                    //rest.searchMenuItem(searchingText);
                    //  itemNameSelector.removeItemAt(0);
                    // itemNameSelector.revalidate();


                } catch (BadLocationException ex) {
                    Logger.getLogger(ExternalOrderPanel.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

            public void changedUpdate(DocumentEvent e) {
                throw new UnsupportedOperationException("Not supported yet.");
            }
        });

        javax.swing.SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                rest.searchMenuItem(searchingText);
                itemNameSelector.addItem(searchingText);
                itemNameSelector.revalidate();
                itemNameSelector.repaint();

            }
        });

           final JTextComponent tc = (JTextComponent) itemNameSelector.getEditor().getEditorComponent();
            tc.getDocument().addDocumentListener(new DocumentListener() {

                public void insertUpdate(DocumentEvent e) {
                    try {
                        searchingText = e.getDocument().getText(0, e.getDocument().getLength());
                        System.out.println("SEARCH:" + searchingText);

                    } catch (BadLocationException ex) {
                        Logger.getLogger(ExternalOrderPanel.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }

                public void removeUpdate(DocumentEvent e) {
                    try {
                        searchingText = e.getDocument().getText(0, e.getDocument().getLength());
                        System.out.println("SEARCH:" + searchingText);
                        //rest.searchMenuItem(searchingText);
                        //  itemNameSelector.removeItemAt(0);
                        // itemNameSelector.revalidate();


                    } catch (BadLocationException ex) {
                        Logger.getLogger(ExternalOrderPanel.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }

                public void changedUpdate(DocumentEvent e) {
                    throw new UnsupportedOperationException("Not supported yet.");
                }
            });

            javax.swing.SwingUtilities.invokeLater(new Runnable() {

                public void run() {
                    rest.searchMenuItem(searchingText);
                    itemNameSelector.addItem(searchingText);
                    itemNameSelector.revalidate();
                    itemNameSelector.repaint();

                }
            });
47pirates
Junior Poster
129 posts since Dec 2009
Reputation Points: 19
Solved Threads: 1
Skill Endorsements: 0

Look at all the places where you try to change the contents and replace the remove or add statements with an invokeLater that runs the remove or add later.

JamesCherrill
... trying to help
Moderator
8,666 posts since Apr 2008
Reputation Points: 2,636
Solved Threads: 1,476
Skill Endorsements: 33

I modified my code like this but it goes on adding items infinitely and also the editor pane is filled with "word" the w is typed.

final Runnable run1 = new Runnable() {

            public void run() {
                itemNameSelector.removeAllItems();
                rest.searchMenuItem(searchingText);
                int i = RestuarantServiceImpl.menus.size();
                for (int k = 1; k < i; k++) {
                    itemNameSelector.addItem(RestuarantServiceImpl.menus.get(k));
                }
                itemNameSelector.revalidate();
                itemNameSelector.repaint();
            }
        };

        final JTextComponent tc = (JTextComponent) itemNameSelector.getEditor().getEditorComponent();
        tc.getDocument().addDocumentListener(new DocumentListener() {

            public void insertUpdate(DocumentEvent e) {
                try {

                    searchingText = e.getDocument().getText(0, e.getDocument().getLength());
                    System.out.println("SEARCH:" + searchingText);
                     javax.swing.SwingUtilities.invokeLater(run1);


                } catch (BadLocationException ex) {
                    Logger.getLogger(ExternalOrderPanel.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

            public void removeUpdate(DocumentEvent e) {
                try {

                    searchingText = e.getDocument().getText(0, e.getDocument().getLength());
                    System.out.println("SEARCH:" + searchingText);
                      javax.swing.SwingUtilities.invokeLater(run1);

                } catch (BadLocationException ex) {
                    Logger.getLogger(ExternalOrderPanel.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
47pirates
Junior Poster
129 posts since Dec 2009
Reputation Points: 19
Solved Threads: 1
Skill Endorsements: 0

By updating the document you are triggering your listener - which updates the document - ...
One way to avoid this is to have a shared boolean that you set when you do a programmatic update (ie in the invokeLater's run) then test this in the document listener to decide whether to do anything (and, of course, reset the boolean).
(I'm going offline now, so you may need some trial-and-error to get it right.)

JamesCherrill
... trying to help
Moderator
8,666 posts since Apr 2008
Reputation Points: 2,636
Solved Threads: 1,476
Skill Endorsements: 33

i'm not getting the result will u please suggest me using some code.

47pirates
Junior Poster
129 posts since Dec 2009
Reputation Points: 19
Solved Threads: 1
Skill Endorsements: 0

i'm not getting the result

What result are you not getting? What code did you write?

JamesCherrill
... trying to help
Moderator
8,666 posts since Apr 2008
Reputation Points: 2,636
Solved Threads: 1,476
Skill Endorsements: 33

i wrote exactly the same as above mentioned code

47pirates
Junior Poster
129 posts since Dec 2009
Reputation Points: 19
Solved Threads: 1
Skill Endorsements: 0

So you haven't tried to implement what I suggested?
OK.

JamesCherrill
... trying to help
Moderator
8,666 posts since Apr 2008
Reputation Points: 2,636
Solved Threads: 1,476
Skill Endorsements: 33
final Runnable run1 = new Runnable() {

            public void run() {
                while (sts == true) {
                    itemNameSelector.removeAllItems();
                    rest.searchMenuItem(searchingText);
                    int i = RestuarantServiceImpl.menus.size();
                    for (int k = 1; k < i; k++) {
                        itemNameSelector.addItem(RestuarantServiceImpl.menus.get(k));
                    }

                    sts = false;
                    itemNameSelector.revalidate();
                    itemNameSelector.repaint();
                }
            }
        };



        final JTextComponent tc = (JTextComponent) itemNameSelector.getEditor().getEditorComponent();
        tc.getDocument().addDocumentListener(new DocumentListener() {

            public void insertUpdate(DocumentEvent e) {
                try {
                    sts = true;
                    searchingText = e.getDocument().getText(0, e.getDocument().getLength());
                    System.out.println("SEARCH:" + searchingText);
                    javax.swing.SwingUtilities.invokeLater(run1);



                } catch (BadLocationException ex) {
                    Logger.getLogger(ExternalOrderPanel.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

            public void removeUpdate(DocumentEvent e) {
                try {
                    sts = true;
                    searchingText = e.getDocument().getText(0, e.getDocument().getLength());
                    System.out.println("SEARCH:" + searchingText);
                    javax.swing.SwingUtilities.invokeLater(run1);

                } catch (BadLocationException ex) {
                    Logger.getLogger(ExternalOrderPanel.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

            public void changedUpdate(DocumentEvent e) {
                throw new UnsupportedOperationException("Not supported yet.");
            }
        });

        this is how i tried to program by usinbg boolean as u suggested but  its not working. Whenever i press C, the 1st word of arraylist is automatically completed in that searchbox and then when i try to delete the letters i am not able to delete them. please help
47pirates
Junior Poster
129 posts since Dec 2009
Reputation Points: 19
Solved Threads: 1
Skill Endorsements: 0

Your while loop in run1 will never terminate, so your code is stuck in an infinite loop.

Your document listener will be called when the document changes. This may be because the user has typed something, or becuase your code has changed it. If the user has changed it you want to perform yopur updates, but not in response to your code changing it.
So you need a shared boolean.
When you update the document (ie in your run1 class) you set the boolean to show that this is a programmatic change, not a user change.
When you respond to changes you check the boolean. If it's fase then you are responding to user input and can continue as normal. If it;s true you are responding to a programmed change, and don't want to do anything else (except reset the boolean).

JamesCherrill
... trying to help
Moderator
8,666 posts since Apr 2008
Reputation Points: 2,636
Solved Threads: 1,476
Skill Endorsements: 33

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page generated in 0.8346 seconds using 2.75MB