0

私が欲しいのは、編集可能な jcombobox です。ユーザーが入力すると、データベースが検索され、ユーザーが入力したテキストで始まるデータベース内の名前が表示されます。例えば。: ユーザーが「a」と入力した場合。次に、 jcombobox は、データベース内の 'a' で始まるすべての名前を表示する必要があります。

データベースに aaron,aidan,kim という名前が含まれている場合。ユーザーが「a」と入力すると、コンボボックスは名前 aaron と aidan を提案する必要があります

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package Main;

    /**
     *
     * @author John
     */
     import javax.swing.event.*;
     import java.awt.event.*;
     import java.sql.Connection;
     import java.sql.DriverManager;
     import java.sql.ResultSet;
     import java.sql.Statement;
     import java.util.ArrayList;
     import java.util.Vector;
     import javax.swing.DefaultComboBoxModel;
     import javax.swing.JOptionPane;
     import javax.swing.MutableComboBoxModel;
     import javax.swing.text.JTextComponent;

     public class test extends javax.swing.JFrame  {

/**
 * Creates new form test
 */
public test() {


    initComponents();



 ((JTextComponent)        jComboBox1.getEditor().getEditorComponent()).getDocument().addDocumentListener(new DocumentListener() {




        @Override
        public void insertUpdate(DocumentEvent e) {

            String tmp = jComboBox1.getEditor().getItem().toString();
            ArrayList<String> scripts = new ArrayList<String>();
            String str1="";
    try
    {





    String str="SELECT item FROM item WHERE item  LIKE '"+tmp+"%'";

     Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
     Connection con=DriverManager.getConnection("jdbc:odbc:ds1", "sa" , "creative");
     Statement stmt=con.createStatement();
     ResultSet rs=stmt.executeQuery(str);
     while(rs.next())
     {

      str1=rs.getString("item");
      scripts.add(str1);  
      jComboBox1.addItem(str1);

     }





    }
    catch(Exception ex)
    {
     System.out.println("error"+ex);   
    }

        }

        @Override
        public void removeUpdate(DocumentEvent e) {
            String tmp = jComboBox1.getEditor().getItem().toString();

        }

        @Override
        public void changedUpdate(DocumentEvent e) {

        }


    });






}




/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    jComboBox1 = new javax.swing.JComboBox();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jComboBox1.setEditable(true);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(105, 105, 105)
            .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 240, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(141, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(86, 86, 86)
            .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(194, Short.MAX_VALUE))
    );

    pack();
}// </editor-fold>                        

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new test().setVisible(true);
        }
    });
}
// Variables declaration - do not modify                     
private javax.swing.JComboBox jComboBox1;
// End of variables declaration                   


}

これを実行すると、入力した文字で始まるデータベース内の最初の名前のみが jcombobox によって表示されます。errorjava.lang.IllegalStateException: Attempt to mutate in notifications に続くエラー

4

1 に答える 1