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
}
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.