java的组合框自动完成 [英] java comboBox autocomplete
本文介绍了java的组合框自动完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
谁能告诉我如何改变这种code有autoselection和我能写不存在的项目,因为它没有让我写新的项目。
公共类ComboBoxFill扩展PlainDocument
{
ComboBoxModel中模型;
JComboBox的组合框=新的JComboBox();
JTextComponent的编辑;公共ComboBoxFill(ComboBoxModel中的模型,JComboBox的组合框,JTextComponent的编辑)
{
this.model =模型;
this.comboBox =组合框;
this.editor =编辑;
}
公共无效insertString(INT取舍,字符串str,AttributeSet中的一个)抛出BadLocationException的
{
串currentText =的getText(0,的getLength());
串beforeOffset = currentText.substring(0,取舍);
字符串afterOffset = currentText.substring(取舍,currentText.length());
字符串futureText = beforeOffset + STR + afterOffset; 对象项目= lookupItem(futureText);
如果(项目!= NULL)
{
comboBox.setSelectedItem(项目);
}其他
{
项目= comboBox.getSelectedItem();
取舍=取舍-str.length();
comboBox.getToolkit()音()。
} super.remove(0,的getLength());
super.insertString(0,item.toString(),一); 如果(item.toString()等于(STR)及&放大器;取舍== 0)
{
highlightCompletedText(0);
}其他{
highlightCompletedText(权衡+ str.length());
comboBox.setPopupVisible(真);
}
}
私人布尔startsWithIgnoreCase(字符串STR1,字符串STR2){
返回str1.toUpperCase()startsWith(str2.toUpperCase())。
}
私人无效highlightCompletedText(INT启动)
{
editor.setCaretPosition(的getLength());
editor.moveCaretPosition(开始);
}私有对象lookupItem(字符串模式)
{
对象将selectedItem = model.getSelectedItem();
如果(将selectedItem = NULL&放大器;!&安培; startsWithIgnoreCase(selectedItem.toString(),模式)){
返回将selectedItem;
}其他{
的for(int i = 0,N = model.getSize(); I< N;我++){
对象CURRENTITEM = model.getElementAt(ⅰ);
如果(startsWithIgnoreCase(currentItem.toString(),图案))
{
返回CURRENTITEM;
}
}
}
返回null;}
}
我把这种方式:
的JTextComponent editor3 =(JTextComponent的)comboBox_2.getEditor()getEditorComponent()。
editor3.setDocument(新ComboBoxFill(comboBox_2.getModel(),comboBox_2,editor3));
解决方案
Java2s有这样一个例子:的 AutocompleteComboBox
Can someone tell me how to change this code to have autoselection and I could be able to write not existing items, because it doesnt let me to write new items.
public class ComboBoxFill extends PlainDocument
{
ComboBoxModel model;
JComboBox comboBox=new JComboBox();
JTextComponent editor;
public ComboBoxFill(ComboBoxModel model, JComboBox comboBox,JTextComponent editor)
{
this.model = model;
this.comboBox=comboBox;
this.editor=editor;
}
public void insertString (int offs,String str,AttributeSet a) throws BadLocationException
{
String currentText=getText(0,getLength());
String beforeOffset=currentText.substring(0,offs);
String afterOffset=currentText.substring(offs,currentText.length());
String futureText = beforeOffset+str+afterOffset;
Object item =lookupItem(futureText);
if (item!=null)
{
comboBox.setSelectedItem(item);
}else
{
item = comboBox.getSelectedItem();
offs=offs-str.length();
comboBox.getToolkit().beep();
}
super.remove(0, getLength());
super.insertString(0, item.toString(), a);
if (item.toString().equals(str)&&offs==0)
{
highlightCompletedText(0);
}else{
highlightCompletedText(offs+str.length());
comboBox.setPopupVisible(true);
}
}
private boolean startsWithIgnoreCase(String str1, String str2) {
return str1.toUpperCase().startsWith(str2.toUpperCase());
}
private void highlightCompletedText(int start)
{
editor.setCaretPosition (getLength());
editor.moveCaretPosition(start);
}
private Object lookupItem(String pattern)
{
Object selectedItem=model.getSelectedItem();
if (selectedItem!=null && startsWithIgnoreCase(selectedItem.toString(), pattern)){
return selectedItem;
}else{
for (int i=0, n=model.getSize();i<n;i++){
Object currentItem = model.getElementAt(i);
if (startsWithIgnoreCase(currentItem.toString(),pattern))
{
return currentItem;
}
}
}
return null;
}
}
I call it this way:
JTextComponent editor3 = (JTextComponent) comboBox_2.getEditor().getEditorComponent();
editor3.setDocument(new ComboBoxFill(comboBox_2.getModel(),comboBox_2, editor3));
解决方案
Java2s has an example for this: AutocompleteComboBox
这篇关于java的组合框自动完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文