为大型昂贵的数据集摆动自动完成

时间:2013-04-08 23:20:53

标签: java swing autocomplete

我正在尝试为Swing JTextField实现自动完成功能,其中可能的自动填充条目集可能很大(10-100K项)并且检索起来很昂贵,但搜索条目集相当便宜。任何人都可以指出一个很好的解决方案,最好是图书馆形式?我查看了SwingX autocomplete,但它不适用于您没有索引访问权限的情况。


更新:由于显然不清楚,问题不在于搜索大量条目是昂贵的(事实并非如此),而是获得整套条目在这种特殊情况下,条目是昂贵且不切实际的。

2 个答案:

答案 0 :(得分:1)

我和Glazed Lists好运。这假设您可以将整个数据集加载到内存中并保留在那里。我用20K左右的物品完成了它。

答案 1 :(得分:0)

我最终在Autocompletion in Swing找到了SamuelSjöberg的“this answer”,并在其基础上实施了以下解决方案。

public class MyAutocompleteDocument extends DefaultStyledDocument {

  @Override
  public void insertString ( int offs, String str, AttributeSet a ) throws BadLocationException
  {
    if ( !StringUtils.isBlank( str ) )
    {
        String text = getText( 0, offs ); // Current text up to insert point
        String completion = complete( text + str ); // Full completed string

        if (!StringUtils.isBlank( completion )) { // not "" or null
            String textToInsert = completion.substring( offs );

            // Always overwrite on complete
            int oldLength = getLength();
            remove( offs, oldLength - offs );
            super.insertString( offs, textToInsert, a );

            // Select the auto-completed portion so it's easy to keep typing
            owner.select( offs + str.length(), getLength() );
        } else {
            super.insertString( offs, str, a );
        }
    }
  }

  private String complete(String startsWith) {
      // access service to search for completion
  }
}

不确定它是否适用于所有情况,但它似乎涵盖了我的。

相关问题