1,105,395 Community Members

Updating Jcombobox on runtime.

Member Avatar
47pirates
Junior Poster
129 posts since Dec 2009
Reputation Points: 9 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
nmaillet
Posting Pro
535 posts since Aug 2008
Reputation Points: 97 [?]
Q&As Helped to Solve: 106 [?]
Skill Endorsements: 7 [?]
 
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"; } });
...
Member Avatar
47pirates
Junior Poster
129 posts since Dec 2009
Reputation Points: 9 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

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*
Member Avatar
47pirates
Junior Poster
129 posts since Dec 2009
Reputation Points: 9 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
stultuske
Posting Expert
5,326 posts since Jan 2007
Reputation Points: 938 [?]
Q&As Helped to Solve: 776 [?]
Skill Endorsements: 35 [?]
Featured
 
0
 

can you show the code where that exception is thrown?

Member Avatar
47pirates
Junior Poster
129 posts since Dec 2009
Reputation Points: 9 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 
 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.

Member Avatar
JamesCherrill
... trying to help
10,383 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,751 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
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.

Member Avatar
nmaillet
Posting Pro
535 posts since Aug 2008
Reputation Points: 97 [?]
Q&As Helped to Solve: 106 [?]
Skill Endorsements: 7 [?]
 
-1
 

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.

Member Avatar
47pirates
Junior Poster
129 posts since Dec 2009
Reputation Points: 9 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

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();

                }
            });
Member Avatar
JamesCherrill
... trying to help
10,383 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,751 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
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.

Member Avatar
47pirates
Junior Poster
129 posts since Dec 2009
Reputation Points: 9 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

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);
                }
            }
Member Avatar
JamesCherrill
... trying to help
10,383 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,751 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
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.)

Member Avatar
47pirates
Junior Poster
129 posts since Dec 2009
Reputation Points: 9 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
JamesCherrill
... trying to help
10,383 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,751 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
0
 

i'm not getting the result

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

Member Avatar
47pirates
Junior Poster
129 posts since Dec 2009
Reputation Points: 9 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

i wrote exactly the same as above mentioned code

Member Avatar
JamesCherrill
... trying to help
10,383 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,751 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
0
 

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

Member Avatar
47pirates
Junior Poster
129 posts since Dec 2009
Reputation Points: 9 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 
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
Member Avatar
JamesCherrill
... trying to help
10,383 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,751 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
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).

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: