0

Im using an Autosuggerstor textField posted on stackoverflow at Create a autocompleting textbox in Java with a dropdown list

This really nice component allow you to select from a list of matching words in a dictionary to write faster and with correct sintax (a sort of intellisense)

I was trying to add the feature of automatic autocompletition when a typed word has only one match and spacebar is hitten.

The following should do the trick

private void checkForAndShowSuggestions() {
    String text=textField.getText();

    boolean hitSpace=false;

    if (text.contains(" "))
        hitSpace =  text.lastIndexOf(" ")==text.length()-1;
    if(labelz.size()==1&&hitSpace){
        labelz.get(0).replaceWithSuggestedText();
        return;
    }

where labelz contains the last list of suggestions.

but i get the error

Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: Attempt to mutate in notification
    at javax.swing.text.AbstractDocument.writeLock(AbstractDocument.java:1338)
    at javax.swing.text.AbstractDocument.replace(AbstractDocument.java:658)
    at javax.swing.text.JTextComponent.setText(JTextComponent.java:1669)
    at ygg.desktop.vm.components.SuggestionLabel.replaceWithSuggestedText(SuggestionLabel.java:89)
    at ygg.desktop.vm.components.AutoSuggestor.checkForAndShowSuggestions(AutoSuggestor.java:183)
    at ygg.desktop.vm.components.AutoSuggestor.access$0(AutoSuggestor.java:174)
    at ygg.desktop.vm.components.AutoSuggestor$1.insertUpdate(AutoSuggestor.java:44)
    at javax.swing.text.AbstractDocument.fireInsertUpdate(AbstractDocument.java:201)
    at javax.swing.text.AbstractDocument.handleInsertString(AbstractDocument.java:748)
    at javax.swing.text.AbstractDocument.insertString(AbstractDocument.java:707)
    at javax.swing.text.PlainDocument.insertString(PlainDocument.java:130)
    at javax.swing.text.AbstractDocument.replace(AbstractDocument.java:669)
    at javax.swing.text.JTextComponent.replaceSelection(JTextComponent.java:1328)
    at javax.swing.text.DefaultEditorKit$DefaultKeyTypedAction.actionPerformed(DefaultEditorKit.java:884)
    at javax.swing.SwingUtilities.notifyAction(SwingUtilities.java:1663)
    at javax.swing.JComponent.processKeyBinding(JComponent.java:2882)
    at javax.swing.JComponent.processKeyBindings(JComponent.java:2929)
    at javax.swing.JComponent.processKeyEvent(JComponent.java:2845)
    at java.awt.Component.processEvent(Component.java:6310)
    at java.awt.Container.processEvent(Container.java:2236)
    at java.awt.Component.dispatchEventImpl(Component.java:4889)
    at java.awt.Container.dispatchEventImpl(Container.java:2294)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1954)
    at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:806)
    at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:1074)
    at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:945)
    at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:771)
    at java.awt.Component.dispatchEventImpl(Component.java:4760)
    at java.awt.Container.dispatchEventImpl(Container.java:2294)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.awt.EventQueue$4.run(EventQueue.java:729)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

Apparently i cant change the textfield inside this thread, is there any way around it? like adding the labelz.get(0).replaceWithSuggestedText(); code to some sort of queue?

here is where the exception generates in my source

void replaceWithSuggestedText() {
    String suggestedWord = getText(); 
    String text = textField.getText(); 
    String typedWord = autoSuggestor.getCurrentlyTypedWord(); 
    String t = text.substring(0, text.lastIndexOf(typedWord)); 
    String tmp = t + text.substring(text.lastIndexOf(typedWord)).replace(typedWor‌​d, suggestedWord); 
    textField.setText(tmp + " ");//exception line
    }
Community
  • 1
  • 1
J dev
  • 66
  • 1
  • 9
  • The error occurs in `replaceWithSuggestedText`, please provide its code or better yet, a [mcve] . – Arnaud Jan 11 '17 at 15:52
  • void replaceWithSuggestedText() { String suggestedWord = getText(); String text = textField.getText(); String typedWord = autoSuggestor.getCurrentlyTypedWord(); String t = text.substring(0, text.lastIndexOf(typedWord)); String tmp = t + text.substring(text.lastIndexOf(typedWord)).replace(typedWord, suggestedWord); textField.setText(tmp + " ");//exception line } – J dev Jan 11 '17 at 16:06
  • how do i edit? the full code is reported in the link at the top, i only added the lines i posted – J dev Jan 11 '17 at 16:07
  • Your error stack shows that you are using a `DocumentListener`, check this question : http://stackoverflow.com/questions/2788779/java-lang-illegalstateexception-while-using-document-listener-in-textarea-java and this one http://stackoverflow.com/questions/15206586/getting-attempt-to-mutate-notification-exception – Arnaud Jan 11 '17 at 16:14
  • 1
    thank you! SwingUtilities.invokeLater(Runnable) did the trick, now im getting ifinite autocompletes XD but this i can figure out – J dev Jan 11 '17 at 16:28
  • Have a look at the second answer from the first link, it explains how to deal with that. – Arnaud Jan 11 '17 at 16:35
  • thanks, i added a boolean to the label and i use it now in the line if(labelz.size()==1&&hitSpace&&!labelz.get(0).justAutoCompleted) and did some changes here and there to get the correct amount of spaces.now it works fine for the most... will look into that tomorrow. – J dev Jan 11 '17 at 17:53

0 Answers0