0
我使用以下代碼將數據從我的數據庫檢索到JComboBox
。它工作正常。我的問題是當我執行這個代碼時,如果我單擊jCombobox中的任何項目,我需要StudId
用於文本框中的單擊項目,但此代碼僅返回第一個元素的Id。當我點擊另一個項目時,文本框的值不會更改。如何在此代碼中使用項目更改選項?將mysql數據庫值檢索到JComboBox
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
}
下一次刪除已註釋的代碼,如果它與該問題無關。並簡化您的代碼到最低限度,以顯示您的麻煩。 – SJuan76 2012-01-03 08:21:56