0

My requirement is to use addDocumentListener, the doSearchCmb basically narrows down items in combobox, function is working if keypressed is used. If I remove the function Runnable doSearchCmb and put the narrowing down of items in insertUpdate without using invokeLater, I get an error of 'Attempt to mutate notification' exception.

In my current code, my screen freezes after I type a letter. After waiting several minutes, I get the error of java.lang.OutOfMemoryError: Java heap space. I tried to add return; after combo.repaint();, my screen didn't freeze, there's no java heap space error but nothing happened at all. I attached the code without the return.

What can I do here to remain the use of addDocumentListener and the function which narrows down the items of the combobox?

TCombo combo = new TCombo();
JTextComponent editor = (JTextComponent) combo.getEditor().getEditorComponent();
editor.getDocument().addDocumentListener(new DocumentListener() {    
            public void changedUpdate(DocumentEvent arg0) {    
            }   
            public void insertUpdate(DocumentEvent arg0) {
                searchCmb();
            }    
            public void removeUpdate(DocumentEvent arg0) {
                searchCmb();
            }   
            private void searchCmb() {
                Runnable doSearchCmb = new Runnable() {
                    @Override
                    public void run() {
                        String item = combo.getEditor().getItem().toString().trim();
                        boolean isEmpty = item.equals("");
                        CmbElement[] foundList = null;
                        String toFind = "";
                        List list = new ArrayList(0);
                        if (!isEmpty) {
                            combo.removeAllItems();
                            combo.setItems(elements);
                            for (int i = 1; i < elements.length; i++) {
                                if (elements[i].getName().contains(toFind)) {
                                    if (i == 1) {
                                        list.add("");
                                    }
                                    list.add(elements[i]);
                                }
                                foundList = (CmbElement[]) list.toArray(new CmbElement[list.size()]);
                            }
                            if (list.size() > 0) {
                                combo.removeAllItems();
                                combo.setItems(foundList);
                            } else {
                                combo.removeAllItems();
                                if (toFind.equals("")) {
                                    combo.setItems(elements);
                                }
                                list.add(new DCmbElement("", ""));
                                foundList = (CmbElement[]) list.toArray(new CmbElement[list.size()]);
                                combo.setItems(foundList);
                            }
                            combo.repaint();
                        }
                    }
                };
                SwingUtilities.invokeLater(doSearchCmb);
            }

        });

CmbElement:

public abstract interface CmbElement {
        public abstract String getKey();

        public abstract String getName();
    }

Note: Narrow down items in combo box means when user inputs a letter, or paste a word, the items in combo box gets filtered using the current letter or word as parameter. It searches through the items and narrows it down. For reference the behavior is like the image here: jcombobox filter in java - Look and feel independent

My function indicated in run() is working fine if keypressed of keylistener is used, but my requirement is to use addDocumentListener

Shahid
  • 481
  • 1
  • 8
  • 22
gengencera
  • 426
  • 2
  • 15
  • 2
    That error occurs when you change something in the component that has this document listener. When the listener triggers, then you change the component, so the listener triggers again and again, causing OOM. Maybe you would like to share with us more info, of what you are trying to do, because your code seems a bit complex and the thing you are trying to do sounds easier to be done. I can not understand what you mean "narrow down" the combo box :( – George Z. Oct 05 '18 at 10:18
  • @GeorgeZougianos Narrowing down is like filtering. I updated the question. Thanks – gengencera Oct 05 '18 at 10:24
  • `I get the error of java.lang.OutOfMemoryError: Java heap space` - so where is you debug code? Did you add any System.out.println(...) statement to your code to better understand what your logic is doing and to understand the flow of your code? OutOfMemory means you have a loop of some kind so find out why and then fix the problem. `combo.repaint();` - is not required. Swing components will automatically repaint themselves when a property of the component is changed. – camickr Oct 05 '18 at 14:14
  • Post a proper [mcve] when you post a question. So in this case you need a JFrame with a JComboBox to demonstrate your problem. – camickr Oct 05 '18 at 14:15

0 Answers0