需要有关验证自动完成的帮助
在搜索单行自动完成功能时,我到处都找到了代码,最后最终使用了这个
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您的
Document
实现方向正确+1,您必须添加AttributeSet
和Caret
作为参数,请查看 自动完成 JComboBox/JTextField 有效
I think that your implementations for
Document
going correct direction +1, you have to addAttributeSet
andCaret
as parametersplease look how AutoComplete JComboBox/JTextField works