需要有关验证自动完成的帮助

发布于 2025-01-08 13:05:17 字数 1577 浏览 0 评论 0原文

在搜索单行自动完成功能时,我到处都找到了代码,最后最终使用了这个

public class AutoCompleteDocument extends PlainDocument {

    private final List<String> dictionary = new ArrayList<String>(); 
    private final JTextComponent jTextField;

    public AutoCompleteDocument(JTextComponent field, String[] aDictionary) {
        jTextField = field;
        dictionary.addAll(Arrays.asList(aDictionary));
    }

    @Override
    public void insertString(int offs, String str, AttributeSet a)
            throws BadLocationException {
        super.insertString(offs, str, a);
        String word = autoComplete(getText(0, getLength()));
        if (word != null) {
            super.insertString(offs + str.length(), word, a);
            jTextField.setCaretPosition(offs + str.length());
            jTextField.moveCaretPosition(getLength());
        }
    }

    public String autoComplete(String text) {
        for (Iterator<String> i = dictionary.iterator(); i.hasNext();) {
            String word = i.next();
            if (word.startsWith(text)) {
                return word.substring(text.length());
            }
        }
        return null;
    }  
}

然后我使用它,就像

autoCompleteDoc = new AutoCompleteDocument(myTextField,myDictionary);
myTextField.setDocument(autoCompleteDoc);

一切正常一样。

我的问题如下:

myTextField 有一个 actionPerformed 侦听器,以便当我按 Enter 键时它会进行一些处理。

不幸的是,我想要的是当文本被“提议”(突出显示)时,当我按 Enter 键时,它会验证该提议,以便我可以继续输入,并且只有当没有提议文本(不突出显示)时,然后当我按 Enter 键时,它才会进行处理。

我只是不知道从哪里开始。 有人可以帮我吗?

Searching around for single line auto-completion I have found code here and there and finally ended up using this one

public class AutoCompleteDocument extends PlainDocument {

    private final List<String> dictionary = new ArrayList<String>(); 
    private final JTextComponent jTextField;

    public AutoCompleteDocument(JTextComponent field, String[] aDictionary) {
        jTextField = field;
        dictionary.addAll(Arrays.asList(aDictionary));
    }

    @Override
    public void insertString(int offs, String str, AttributeSet a)
            throws BadLocationException {
        super.insertString(offs, str, a);
        String word = autoComplete(getText(0, getLength()));
        if (word != null) {
            super.insertString(offs + str.length(), word, a);
            jTextField.setCaretPosition(offs + str.length());
            jTextField.moveCaretPosition(getLength());
        }
    }

    public String autoComplete(String text) {
        for (Iterator<String> i = dictionary.iterator(); i.hasNext();) {
            String word = i.next();
            if (word.startsWith(text)) {
                return word.substring(text.length());
            }
        }
        return null;
    }  
}

Then I use this like

autoCompleteDoc = new AutoCompleteDocument(myTextField,myDictionary);
myTextField.setDocument(autoCompleteDoc);

Everything works fine.

My problem is the following :

The myTextField has a listener for actionPerformed so that when I press enter key it does some processing.

Unfortunately what I would like is when the text is "proposed" (highlighted), when I press enter it validates the proposition so I can continue typing and only when no text is proposed (no highlight) then when I press enter it does my processing.

I'm simply stuck with no idea where to start from.
Anyone can help me out ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哽咽笑 2025-01-15 13:05:17

我认为您的 Document 实现方向正确+1,您必须添加 AttributeSetCaret 作为参数,

请查看 自动完成 JComboBox/JTextField 有效

I think that your implementations for Document going correct direction +1, you have to add AttributeSet and Caret as parameters

please look how AutoComplete JComboBox/JTextField works

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文