SwingUtilities.invokeLater does nothing

134 Views Asked by At

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

0

There are 0 best solutions below