swingx
  1. swingx
  2. SWINGX-1475

Autocomplete not working as expected

    Details

    • Type: Bug Bug
    • Status: Open
    • Priority: Major Major
    • Resolution: Unresolved
    • Affects Version/s: 1.6.2
    • Fix Version/s: None
    • Component/s: Autocomplete
    • Labels:
      None
    • Environment:

      Windows 7 x64

      Description

      I'm using Autocomplete with a JTextField this way:

      JTextField txt = new JTextField();
      List<String> items = Arrays.asList(new String[]

      {"One", "two", "Three"}

      );
      AbstractAutoCompleteAdaptor adaptor = new TextComponentAdaptor(this, items);
      AutoCompleteDocument document = new AutoCompleteDocument(adaptor, false, ObjectToStringConverter.DEFAULT_IMPLEMENTATION, txt.getDocument());

      AutoCompleteDecorator.decorate(txt, document, adaptor);

      Now I want to write "there" into the textfield. This cannot be done because if the AutoCompleteDocument finds a recommendation this will be used. So "Three" will be recommended. On the other hand, if I enter "tree", move with the cursor between t and r and insert a h the entire text will be replaced by the recommendation "Three". This is very annoying to the user. I've changed the AutoCompleteDocument-Implementation a litte bit (only the insertString-method):

      @Override
      public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
      // return immediately when selecting an item
      if (selecting) return;
      // insert the string into the document
      delegate.insertString(offs, str, a);

      if (offs < getLength() - 1)

      { // insert before end --> no autocomplete setSelectedItem(null, null); return; }

      // lookup and select a matching item
      LookupResult lookupResult;
      String pattern = getText(0, getLength());

      if (pattern == null || pattern.length() == 0)

      { lookupResult = new LookupResult(null, ""); setSelectedItem(lookupResult.matchingItem, lookupResult.matchingString); }

      else

      { lookupResult = lookupItem(pattern); }

      if (lookupResult.matchingItem != null)

      { setSelectedItem(lookupResult.matchingItem, lookupResult.matchingString); }

      else {
      if (strictMatching) {
      // keep old item selected if there is no match
      lookupResult.matchingItem = adaptor.getSelectedItem();
      lookupResult.matchingString = adaptor.getSelectedItemAsString();
      // imitate no insert (later on offs will be incremented by
      // str.length(): selection won't move forward)
      offs = str == null ? offs : offs - str.length();

      if (str != null && !str.isEmpty())

      { // provide feedback to the user that his input has been received but can not be accepted UIManager.getLookAndFeel().provideErrorFeedback(adaptor.getTextComponent()); }

      } else

      { // no item matches => use the current input as selected item lookupResult.matchingItem = getText(0, getLength()); lookupResult.matchingString = getText(0, getLength()); setSelectedItem(lookupResult.matchingItem, lookupResult.matchingString); }

      }

      // select the completed part
      int len = str == null ? 0 : str.length();
      offs = lookupResult.matchingString == null ? 0 : offs + len;

      // only append the matching String, leave the input
      delegate.insertString(offs, lookupResult.matchingString.substring(offs), null);

      adaptor.markText(offs);
      }

      Please integrate this into your code, thank you very much!

        Activity

        There are no comments yet on this issue.

          People

          • Assignee:
            Karl Schaefer
            Reporter:
            kotzbrocken2
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated: