http://java.sun.com/docs/books/tutorial/uiswing/components/combobox.html

解决方案 »

  1.   

    多谢 gdsean(摇滚java的提示:
    但现在我还是有很多的问题。Jcombox1.setEditable(true)后,很多在不可编辑的combox中可以触发的事件比如说获得焦点,失去焦点,请问这些如何添加?
      

  2.   

    参考这个类。
    package Common;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.plaf.basic.*;
    public class CustomComboEditor extends BasicComboBoxEditor {
      int rowCount;
      String[] names;
      public CustomComboEditor( final String[] names, final int rowCount ) {
        super();
        this.names = names;
        this.rowCount = rowCount;
        editor.addKeyListener( new KeyAdapter() {
          public void keyReleased( KeyEvent e ) {
            if (e.getKeyCode() == KeyEvent.VK_ENTER)
              processEvent();
          }
        });    editor.addActionListener( new ActionListener(){
          public void actionPerformed(ActionEvent e) {
            processEvent();
          }
        });    editor.addFocusListener( new FocusAdapter() {
          public void focusGained( FocusEvent e) {
          ((JTextField) e.getComponent()).selectAll();
          }
          public void focusLost( FocusEvent e) {
            processEvent();
          }
        });
      }  void processEvent() {
        String wordTyped = editor.getText();
        for ( int i = 0; i < rowCount; i++ ) {
          if (( (String)names[ i ] ).toUpperCase().startsWith( wordTyped.toUpperCase() ) ) {
            editor.selectAll();
            editor.setText( (String)names[ i ] );
            editor.setSelectionStart( wordTyped.length() );
            break;
          }
        }
      }
    }