JFrame 中自动建议的组合框

发布于 2024-12-23 19:23:26 字数 655 浏览 2 评论 0原文

我使用以下代码将数据检索到组合框中。如何将 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);

                    }

I use the following code for retrieve the data into combobox. How to change Jcombobox into Auto suggested combobox . It means when I press A the words which are start with A that will show in the items field in combobox.

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

                    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

人间☆小暴躁 2024-12-30 19:23:26

尝试使用包 org.jdesktop.swingx.autocomplete,maven 存储库 此处

Try to use Combobox component from package org.jdesktop.swingx.autocomplete, maven repository here

℉絮湮 2024-12-30 19:23:26
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;
                            }
                          }
                        }
          } );
        }
    }

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

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文