0

これまでのところ動作しているように見えた JTextFields のオートコンプリートがありますが、以下に示す特定のサブケースが表示されました: 「em」を挿入してみてください。リスト (2 つの項目のみを含むリスト:

  1. エマヌエレ・セマニ
  2. マヌメリ・リー

)

私は名前が奇妙であることを知っていますが、2冊の本はこれらの名前を持っていました..

import java.util.ArrayList;
import javax.swing.*;
import javax.swing.text.Document;
import AutoCompleteDocument;
import CompletionService;

public class AutoCompleteExample
{
    TitleService titleService;
    Document titleAutoCompleteDocument;
    JTextField titleField;

    private class TitleService extends CompletionService
    {
        public TitleService()
        {
            ArrayList<String> eventTitles = getBookTitles();
            for (int i = 0; eventTitles != null && i < eventTitles.size(); i++)
            {
                if (eventTitles.get(i) != null)
                {
                    data.add(eventTitles.get(i));
                }
            }
        }
    }

    public AutoCompleteExample()
    {
        JPanel panel = new JPanel();
        titleField = new JTextField();
        titleField.setColumns(50);
        panel.add(titleField);

        titleService = new TitleService();
        titleAutoCompleteDocument = new AutoCompleteDocument(titleService,
                titleField);

         JFrame frame = new JFrame();
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         frame.add(panel);
         frame.setSize(700, 100);
         frame.setVisible(true);
         refreshCompletionServices();
    }

    public void refreshCompletionServices()
    {
        titleService = new TitleService();
        titleAutoCompleteDocument = new AutoCompleteDocument(titleService,
                titleField);
        titleField.setDocument(titleAutoCompleteDocument);
    }


    private ArrayList<String> getBookTitles()
    {
        ArrayList<String> titles = new ArrayList<>();
        titles.add("emanule semani");
        titles.add("manuemeli li");
        return titles;
    }

    public static void main(String args[])
    {
        AutoCompleteExample ex = new AutoCompleteExample();
    }
}

CompletionService.java クラスは

import java.util.ArrayList;
import java.util.List;


public class CompletionService
{
    public List<String> data = new ArrayList<>();
    String autoComplete(String partialString)
    {
        String hit = null;
        for (String o : data)
        {
            if (o.toLowerCase().contains(partialString.toLowerCase()))
            {
                // CompletionService contract states that we only
                // should return completion for unique hits.
                if (hit == null)
                {
                    int index = o.toLowerCase().indexOf(partialString.toLowerCase());
                    hit = o.substring(index);
                }
                else
                {
                    if (hit.length() > o.length())
                    {
                        hit = o;
                    }
                }
            }
        }
        return hit;
    }
}

AUtoCompleteDocument.java は

import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.JTextComponent;
import javax.swing.text.PlainDocument;

public class AutoCompleteDocument extends PlainDocument
{
    private static final long serialVersionUID = 1L;
    private CompletionService completionService;
    private JTextComponent documentOwner;

    public AutoCompleteDocument(CompletionService service,
            JTextComponent documentOwner)
    {
        this.completionService = service;
        this.documentOwner = documentOwner;
    }

    protected String complete(String str)
    {
        Object o = completionService.autoComplete(str);
        return o == null ? null : o.toString();
    }

    @Override
    public void insertString(int offs, String str, AttributeSet a)
            throws BadLocationException
    {
        if (str == null || str.length() == 0)
        {
            return;
        }

        String text = getText(0, offs); // Current text.
        String completion = complete(text + str);
        int length = offs + str.length();
        if (completion != null && text.length() > 0)
        {
            str = completion.substring(length - 1);
            super.insertString(offs, str, a);
            documentOwner.select(length, getLength());
        }
        else
        {
            super.insertString(offs, str, a);
        }
    }
}
4

2 に答える 2

1

交換する必要があります

if (o.toLowerCase().contains(partialString.toLowerCase()))

if (o.toLowerCase().startsWith(partialString.toLowerCase()))
于 2013-03-25T14:53:32.947 に答える
1

私の答えはWhy to reinvent the wheel、使用についてです

于 2013-03-25T14:31:39.513 に答える