我有一个 JComboBox 显示来自数据库 Patient_Details 的名称
public void ComboItem() {
chooser.removeAllItems();
chooser.addItem("Please Select...");
try {
String sql="select * from Patients_Details";
pst = conn.prepareStatement(sql);
rs=pst.executeQuery();
while (rs.next()) {
String id = rs.getString("Patient_ID"); // Get the Id
String name = rs.getString("Name"); // Get the Name
ComboItem comboItem = new ComboItem(id, name); // Create a new ComboItem
chooser.addItem(comboItem); // Put it into the ComboBox
String tmp=comboItem.getid();
}
} catch (SQLException sqle) {
System.out.println(sqle);
}
}
这是来自comboitem 类,它只返回名称而不是id
public String toString() {
return this.name ;
}
我的问题是如何获取选定项以便可以执行此操作我不知道如何执行此操作我已经尝试所有代码近 2 小时任何帮助将不胜感激
注意我是Java初学者
private void chooserPopupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {
try{
String sql="select * from Patients_Details where Patient_ID=? ";
pst=conn.prepareStatement(sql);
rs=pst.executeQuery();
if(rs.next()){
String add1=rs.getString("Patient_ID");
txtpatientid.setText(add1);
String add2=rs.getString("Name");
txtname.setText(add2);
String add3=rs.getString("Age");
txtage.setText(add3);
String add4=rs.getString("Gender");
txtgender.setText(add4);
String add5=rs.getString("Date");
txtdate.setText(add5);
}
}
catch(Exception e) {
JOptionPane.showMessageDialog(null,e );
}
}