ComboBox自动完成功能(Source)

 
import  java.awt.event. * ;
import  javax.swing. * ;
import  javax.swing.text. * ;

public   class  ComboBoxPopup  extends  PlainDocument  {
    JComboBox comboBox;
    ComboBoxModel model;
    JTextComponent editor;
    
boolean selecting=false;
    
    
public ComboBoxPopup(final JComboBox comboBox) {
        
this.comboBox = comboBox;
        model 
= comboBox.getModel();
        editor 
= (JTextComponent) comboBox.getEditor().getEditorComponent();
        comboBox.addActionListener(
new ActionListener() {
            
public void actionPerformed(ActionEvent e) {
                
if (!selecting) highlightCompletedText(0);
            }

        }
);
        editor.addKeyListener(
new KeyAdapter() {
            
public void keyPressed(KeyEvent e) {
                
if (comboBox.isDisplayable()) comboBox.setPopupVisible(true);
            }

        }
);
    }

    
    
public void remove(int offs, int len) throws BadLocationException {
        
if (selecting) return;
        
super.remove(offs, len);
    }

    
    
public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
        
if (selecting) return;
        
super.insertString(offs, str, a);
        Object item 
= lookupItem(getText(0, getLength()));
        
if (item != null{
            setSelectedItem(item);
        }
 else {
            item 
= comboBox.getSelectedItem();
            offs 
= offs-str.length();
            comboBox.getToolkit().beep();
        }

        setText(item.toString());
        highlightCompletedText(offs
+str.length());
    }

    
    
private void setText(String text) throws BadLocationException {
        
super.remove(0, getLength());
        
super.insertString(0, text, null);
    }

    
    
private void highlightCompletedText(int start) {
        editor.setSelectionStart(start);
        editor.setSelectionEnd(getLength());
    }

    
    
private void setSelectedItem(Object item) {
        selecting 
= true;
        model.setSelectedItem(item);
        selecting 
= false;
    }

    
    
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;
    }

    
    
private boolean startsWithIgnoreCase(String str1, String str2) {
        
return str1.toUpperCase().startsWith(str2.toUpperCase());
    }

    
    
private static void createAndShowGUI() {
        JComboBox comboBox 
= new JComboBox(new Object[] {"Ester""Jordi""Jordina""Jorge""Sergi"});
        comboBox.setEditable(
true);
        JTextComponent editor 
= (JTextComponent) comboBox.getEditor().getEditorComponent();
        editor.setDocument(
new ComboBoxPopup(comboBox));
        
        JFrame frame 
= new JFrame();
        frame.setDefaultCloseOperation(
3);
        frame.getContentPane().add(comboBox);
        frame.pack(); frame.setVisible(
true);
    }

    
    
    
public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(
new Runnable() {
            
public void run() {
                createAndShowGUI();
            }

        }
);
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值