将 mysql 数据库值检索到 JComboBox 中

发布于 2024-12-24 16:28:49 字数 4867 浏览 0 评论 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
}

I use the following code to retrieve data from my database to a JComboBox. It was working properly. My problem is when I execute this code, if I click any items in jCombobox I need the StudId for that clicked item in the textbox, but this code returns the first element's Id only. The textbox value isn't changed when I click another item. How to use item changed option in this code?

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
}

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

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

发布评论

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

评论(1

趁年轻赶紧闹 2024-12-31 16:28:49

您将在填充组合框后立即获取 selectedItem。

ob=cbx.getSelectedItem();

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

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

You are getting the selectedItem right after populating the combobox.

ob=cbx.getSelectedItem();

The user has not had time enough to change it

You need to set the logic related to a change of state in the appropiate listener (I think it should be an ActionListener) and add the listener to the comboboxes you want to monitorize.

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