简体   繁体   中英

How to make a java seach & editable combo box

我创建了一个组合框,它从另一个类中加载项目。现在,我想做的是,当我在组合框中键入字母时,应选择与该字母相关的整个项目并列出下来。请任何人告诉我如何做了???

Try this program and modify however you want..

this program displays one text box and matching patterns will be auto populated.

package com.kb;



import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.plaf.basic.*;


public class AutoComboBox
{
      private JComboBox_ cb = null;


    public AutoComboBox() {
        JFrame fr=new JFrame("TEST ComboBox");
    JPanel p = new JPanel();
        p.setLayout( new BorderLayout() );
        String[] ss = new String[]
        {  "112","1123","1124","1134",
           "first",
           "second",
           "third",
           "third 1 before",
           "third 2",
           "third 1 after",
           "third quarter",
           "fourth",
           "fourth and more",
           "fourth and more and more"
        };
    fr.getContentPane().add(p);
        cb = new JComboBox_(ss);

        p.add("South",cb);
        p.add("Center",new JButton("test combo box"));
        fr.pack();
        fr.show();
    }
    public static void main( String[] args ) {
        AutoComboBox test=new AutoComboBox();
    }
}

class JComboBox_ extends JComboBox {
    public int caretPos=0;
    public JTextField tf=null;
    public JComboBox_(final Object items[]) {
        super(items);
        this.setEditor(new BasicComboBoxEditor());
        this.setEditable(true);
    }

    public void setSelectedIndex(int ind) {
        super.setSelectedIndex(ind);
        tf.setText(getItemAt(ind).toString());
        tf.setSelectionEnd(caretPos+tf.getText().length());
        tf.moveCaretPosition(caretPos);
//        tf.setSelectionStart(caretPos);
    }
    public void setEditor(ComboBoxEditor anEditor) {
        super.setEditor(anEditor);
        if (anEditor.getEditorComponent() instanceof JTextField) {
            tf=(JTextField)anEditor.getEditorComponent();
            tf.addKeyListener(new KeyAdapter()
                  {
                        public void keyReleased( KeyEvent ev )
                        {
                          char key=ev.getKeyChar();
                          if (! (Character.isLetterOrDigit(key)||Character.isSpaceChar(key) )) return;
                          String s = tf.getText();
                          caretPos=tf.getCaretPosition();
                          String text="";
                          try {
                            text=tf.getText(0,caretPos);
                          }
                          catch (Exception ex) {
                            ex.printStackTrace();
                          }
                          int n=getItemCount();
                          for (int i=0; i<n; i++) {
                            int ind=((String)getItemAt(i)).indexOf(text);
                            if (ind==0) {
                              setSelectedIndex(i);
                              return;
                            }
                          }
                        }
          } );
        }
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM