将 mysql 数据库值检索到 JComboBox 中
我使用以下代码将数据从数据库检索到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您将在填充组合框后立即获取 selectedItem。
用户没有足够的时间来更改它
您需要在适当的侦听器(我认为它应该是一个 ActionListener)中设置与状态更改相关的逻辑,并将侦听器添加到您想要监视的组合框中。
You are getting the selectedItem right after populating the combobox.
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.