【问题标题】:Auto suggested combobox in JFrameJFrame中的自动建议组合框
【发布时间】:2012-01-03 19:43:47
【问题描述】:

我使用以下代码将数据检索到组合框中。如何将 Jcombobox 更改为自动建议的组合框。这意味着当我按 A 时,以 A 开头的单词将显示在组合框中的项目字段中。

public void combo(){

     try{
                GCS.GContnStr();
                TmpFlxTSt= GCS.GCotnStr.createStatement();
                String select = "Select StudName from studentmaster";
                TmpFlxTRs = TmpFlxTSt.executeQuery(select);

                  while(TmpFlxTRs.next())
                  {
                      String TmpOb1=TmpFlxTRs.getString("StudName");
                      TmpOb1.toString();
                      cbx.addItem(TmpOb1);
                      cbx.setMaximumRowCount(1);

                    }

【问题讨论】:

标签: java swing autocomplete frame jcombobox


【解决方案1】:

尝试使用包org.jdesktop.swingx.autocomplete中的Combobox组件,maven存储库here

【讨论】:

    【解决方案2】:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.tree.*;
    import javax.swing.plaf.basic.*;
    
    
    class Test
    {
          private JComboBox_ cb = null;
    
    
        public Test() {
            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 ) {
          Test test=new Test();
        }
    }
    
    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;
                                }
                              }
                            }
              } );
            }
        }
    
    }
    

    【讨论】:

    • 当然这是个坏名字:-) 重命名为 ... 例如AutoSuggestCombobox
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多