java - Using a value to fire Jcombobox actionlistener instead of getselectedindex? -
here jcombobox has 2 items name , id
public void comboitem() { chooser.removeallitems(); chooser.additem("please select..."); try { string sql="select * patients_details"; pst = conn.preparestatement(sql); rs=pst.executequery(); while (rs.next()) { string id = rs.getstring("patient_id"); // id string name = rs.getstring("name"); // name comboitem comboitem = new comboitem(id, name); // create new comboitem chooser.additem(comboitem); // put combobox string tmp=comboitem.getid(); } } catch (sqlexception sqle) { system.out.println(sqle); }
jcombobox actionlistener code
private void chooserpopupmenuwillbecomeinvisible(javax.swing.event.popupmenuevent evt) { object selectedvalue = chooser.getselectedindex(); try{ string sql="select * patients_details patient_id=? "; pst=conn.preparestatement(sql); pst.setobject(1, selectedvalue); 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 add7=rs.getstring("date"); txtdate.settext(add7); } } catch(exception e) { joptionpane.showmessagedialog(null,e ); } }
my question is: how can use id value fire jcombobox listener instead of getselectedindex appreciated.
use getselecteditem()
obtain reference selected comboitem
, , it's id
; use getselectedindex()
obtain it's index.
because setselectedindex()
fires combo's actionlistener
, navigational controls such ⊲prev , next⊳ buttons can delegate combo, shown here:
obtain
index
getselectedindex()
.increment or decrement
index
.perform range check on
index
.delegate selection combo via
setselectedindex(index)
.
a particular implementation of comboboxmodel
, such defaultcomboboxmodel
, can searched using iteration:
for (int = 0; < dcbm.getsize(); i++) { comboitem item = (comboitem) dcbm.getelementat(i); // check item }
Comments
Post a Comment