0

我使用以下代码将数据从我的数据库检索到JComboBox. 它工作正常。我的问题是当我执行此代码时,如果我单击 jCombobox 中的任何项目,我需要StudId文本框中该单击项目的,但此代码仅返回第一个元素的 Id。当我单击另一个项目时,文本框的值不会改变。如何在此代码中使用项目更改选项?

package designstudent;
import java.awt.*;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.*;
import java.beans.EventHandler;
import javax.swing.table.DefaultTableModel;
import java.sql.*;
import java.util.ArrayList;
import java.util.jar.Attributes.Name;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import java.util.ArrayList.*;
import javax.swing.*;
public class FrmeA extends javax.swing.JFrame  {
     // private JComboBox cbx1;
     // private JLabel lbl1;
      //private ArrayList<String>  Name=new ArrayList<String>();
      //private ArrayList<String> Id=new ArrayList<String>();


      FrmA1 f1=new FrmA1();
      Statement TmpFlxTSt=null;
      ResultSet TmpFlxTRs=null;
      GContnStr GCS=new GContnStr();
      Object ob= new Object();

   public FrmeA() {
         initComponents();
    }


/*public void combo(){

     try{
                GCS.GContnStr();
                TmpFlxTSt= GCS.GCotnStr.createStatement();
                String select = "Select StudName,StudId from studentmaster";
                TmpFlxTRs = TmpFlxTSt.executeQuery(select);
                 while(TmpFlxTRs.next())
                  {
                       Name.add(TmpFlxTRs.getString("StudName"));
                       cbx.addItem(Name);
                       Id.add(TmpFlxTRs.getString("StudId"));
                 }
                for(int i=0;i<Name.size();i++){
                 System.out.println(Name.get(i).toString());
                  }
                 for(int i=0;i<Id.size();i++){
                 System.out.println(Id.get(i).toString());
                  }

                      //String addname=TmpFlxTRs.getString("StudName");
                       //Name.add(addname);
                      //addname.toString();
                      //cbx.addItem(addname);




                    TmpFlxTRs.close();
                    TmpFlxTSt.close();
        }
           catch(Exception e){
               System.out.println(e);

    }

}*/

   public void combo1(){

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

                  while(TmpFlxTRs.next())
                  {
                      String addname=TmpFlxTRs.getString("StudName");
                      addname.toString();

                     cbx.addItem(addname);
                      ob=cbx.getSelectedItem();
                    }
                    //ob=cbx.getSelectedItem();
                    String st=ob.toString();
                    combo2(st);
                    //TmpFlxTRs.close();
                    //TmpFlxTSt.close();
        }
           catch(Exception e){
               System.out.println(e);

    }

}


public void combo2(String Name){

     try{
                GCS.GContnStr();
                TmpFlxTSt= GCS.GCotnStr.createStatement();
                System.out.println(Name);
                String select = "Select StudId from studentmaster where StudName='"+Name+"'";
                TmpFlxTRs = TmpFlxTSt.executeQuery(select);

                  while(TmpFlxTRs.next())
                  {
                      Txt01.setText( TmpFlxTRs.getString("StudId"));
                      System.out.println();
                    }

                    TmpFlxTRs.close();
                    TmpFlxTSt.close();
        }
           catch(Exception e){
               System.out.println(e);

    }

}

private void cbxActionPerformed(java.awt.event.ActionEvent evt) {                                    

}                                   

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                 FrmeA fr=new FrmeA();
                 fr.combo1();
                 //fr.combo1();
                 fr.setVisible(true);
                   //fr.initconn();
            }
        });
    }

    // Variables declaration - do not modify
    private javax.swing.JButton CmdA;
    private javax.swing.JButton CmdD;
    private javax.swing.JButton CmdE;
    private javax.swing.JTable FlxT;
    private javax.swing.JTextField Txt01;
    private javax.swing.JComboBox cbx;
    private javax.swing.JComboBox cbx2;
    private javax.swing.JButton cmdQ;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lbl;
    // End of variables declaration
}
4

1 回答 1

0

填充组合框后,您将立即获得 selectedItem。

ob=cbx.getSelectedItem();

用户没有足够的时间来更改它

您需要在适当的侦听器中设置与状态更改相关的逻辑(我认为它应该是一个 ActionListener),并将侦听器添加到您要监视的组合框中。

于 2012-01-03T08:21:20.533 回答