swing - How to reset JComboBox in Java in case of country field? -
on selecting country field, state field shown, , on selecting state feild, city field shown
but in below code when select first time works fine when select country states gets added previous country's states , similar case city. read jcombobox.removeall() , defaultcomboboxmodel class don't know how use in current case.
so,basically how reset jcombobox items?
have on public countrycode{ } class,
private void stateboxactionperformed,
private void countryboxactionperformed functions
answer:here worked me use statebox.removeallitems() before code r adding states in list database .
here code of netbeans --->
import java.sql.connection; import java.sql.drivermanager; import java.sql.preparedstatement; import java.sql.resultset; import java.sql.sqlexception; import java.util.vector; import java.util.logging.level; import java.util.logging.logger; import javax.swing.defaultcomboboxmodel; import javax.swing.joptionpane; /* * change template, choose tools | templates * , open template in editor. */ /** * * @author system */ public class countrycode extends javax.swing.jframe { defaultcomboboxmodel d1=new defaultcomboboxmodel(); connection con; string code,q; preparedstatement ps; resultset rs; /** * creates new form countrycode */ public countrycode() { initcomponents(); createconnection(); } void createconnection() { try { class.forname("oracle.jdbc.oracledriver");//connection string string r1="jdbc:oracle:thin:@localhost:1521:";//open connection con=drivermanager.getconnection(r1,"system","password"); } catch(exception ex) { joptionpane.showmessagedialog(this,ex); ex.printstacktrace(); } } /** * method called within constructor initialize form. * warning: not modify code. content of method * regenerated form editor. */ @suppresswarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="generated code">//gen-begin:initcomponents private void initcomponents() { countrybox = new javax.swing.jcombobox(); country = new javax.swing.jlabel(); statebox = new javax.swing.jcombobox(); citybox = new javax.swing.jcombobox(); state = new javax.swing.jlabel(); city = new javax.swing.jlabel(); setdefaultcloseoperation(javax.swing.windowconstants.exit_on_close); countrybox.setmodel(new javax.swing.defaultcomboboxmodel(new string[] { "america", "india", "nepal", "pakistan", "sri lanka", " ", " " })); countrybox.addactionlistener(new java.awt.event.actionlistener() { public void actionperformed(java.awt.event.actionevent evt) { countryboxactionperformed(evt); } }); country.settext("country"); statebox.setmodel(new javax.swing.defaultcomboboxmodel(new string[] { " " })); statebox.addactionlistener(new java.awt.event.actionlistener() { public void actionperformed(java.awt.event.actionevent evt) { stateboxactionperformed(evt); } }); state.settext("state"); city.settext("city"); javax.swing.grouplayout layout = new javax.swing.grouplayout(getcontentpane()); getcontentpane().setlayout(layout); layout.sethorizontalgroup( layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgroup(javax.swing.grouplayout.alignment.trailing, layout.createsequentialgroup() .addcontainergap() .addgroup(layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addcomponent(country, javax.swing.grouplayout.default_size, 87, short.max_value) .addcomponent(state, javax.swing.grouplayout.default_size, javax.swing.grouplayout.default_size, short.max_value) .addcomponent(city, javax.swing.grouplayout.default_size, javax.swing.grouplayout.default_size, short.max_value)) .addpreferredgap(javax.swing.layoutstyle.componentplacement.unrelated) .addgroup(layout.createparallelgroup(javax.swing.grouplayout.alignment.leading, false) .addcomponent(citybox, 0, javax.swing.grouplayout.default_size, short.max_value) .addcomponent(statebox, javax.swing.grouplayout.alignment.trailing, 0, javax.swing.grouplayout.default_size, short.max_value) .addcomponent(countrybox, javax.swing.grouplayout.alignment.trailing, 0, 198, short.max_value)) .addgap(95, 95, 95)) ); layout.setverticalgroup( layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgroup(layout.createsequentialgroup() .addgap(50, 50, 50) .addgroup(layout.createparallelgroup(javax.swing.grouplayout.alignment.trailing) .addgroup(layout.createsequentialgroup() .addgroup(layout.createparallelgroup(javax.swing.grouplayout.alignment.baseline) .addcomponent(countrybox, javax.swing.grouplayout.preferred_size, javax.swing.grouplayout.default_size, javax.swing.grouplayout.preferred_size) .addcomponent(country)) .addgap(18, 18, 18) .addcomponent(statebox, javax.swing.grouplayout.preferred_size, javax.swing.grouplayout.default_size, javax.swing.grouplayout.preferred_size)) .addcomponent(state)) .addgap(18, 18, 18) .addgroup(layout.createparallelgroup(javax.swing.grouplayout.alignment.baseline) .addcomponent(citybox, javax.swing.grouplayout.preferred_size, javax.swing.grouplayout.default_size, javax.swing.grouplayout.preferred_size) .addcomponent(city)) .addcontainergap(154, short.max_value)) ); pack(); }// </editor-fold>//gen-end:initcomponents private void countryboxactionperformed(java.awt.event.actionevent evt) {//gen-first:event_countryboxactionperformed try { //get string string coname; coname = (string)countrybox.getselecteditem(); //string query q="select * country cname=? "; //prepare statement ps=con.preparestatement(q); //set string ps.setstring(1,coname); rs=ps.executequery();//resultset while(rs.next()) {d1.getclass(); code=rs.getstring("cocode"); break; } /* ******************* <!------another query------> ****************************** */ //string query q="select * state cocode=? "; //prepare statement ps=con.preparestatement(q); //set string ps.setstring(1,code); resultset rs=ps.executequery(); while(rs.next()) { string code=rs.getstring("stname"); statebox.additem(code); } } catch (exception ex) { ex.printstacktrace(); } }//gen-last:event_countryboxactionperformed private void stateboxactionperformed(java.awt.event.actionevent evt) {//gen-first:event_stateboxactionperformed try { /*---------now query stcode i.e statecode repeat here steps did in country -----------*/ //get string string stname; stname = (string)statebox.getselecteditem(); //string query q="select * state stname=? "; //prepare statement ps=con.preparestatement(q); //set string ps.setstring(1,stname); rs=ps.executequery();//resultset while(rs.next()) { code=rs.getstring("stcode"); break; } /* ******************* <!------another query------> ****************************** */ //string query q="select * city stcode=? "; //prepare statement ps=con.preparestatement(q); //set string ps.setstring(1,code); rs=ps.executequery(); while(rs.next()) { code=rs.getstring("ciname"); citybox.additem(code); } } catch (exception ex) { ex.printstacktrace(); } }//gen-last:event_stateboxactionperformed /** * @param args command line arguments */ public static void main(string args[]) { /* set nimbus , feel */ //<editor-fold defaultstate="collapsed" desc=" , feel setting code (optional) "> /* if nimbus (introduced in java se 6) not available, stay default , feel. * details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { (javax.swing.uimanager.lookandfeelinfo info : javax.swing.uimanager.getinstalledlookandfeels()) { if ("nimbus".equals(info.getname())) { javax.swing.uimanager.setlookandfeel(info.getclassname()); break; } } } catch (classnotfoundexception ex) { java.util.logging.logger.getlogger(countrycode.class.getname()).log(java.util.logging.level.severe, null, ex); } catch (instantiationexception ex) { java.util.logging.logger.getlogger(countrycode.class.getname()).log(java.util.logging.level.severe, null, ex); } catch (illegalaccessexception ex) { java.util.logging.logger.getlogger(countrycode.class.getname()).log(java.util.logging.level.severe, null, ex); } catch (javax.swing.unsupportedlookandfeelexception ex) { java.util.logging.logger.getlogger(countrycode.class.getname()).log(java.util.logging.level.severe, null, ex); } //</editor-fold> /* create , display form */ java.awt.eventqueue.invokelater(new runnable() { public void run() { new countrycode().setvisible(true); } }); } // variables declaration - not modify//gen-begin:variables private javax.swing.jlabel city; private javax.swing.jcombobox citybox; private javax.swing.jlabel country; private javax.swing.jcombobox countrybox; private javax.swing.jlabel state; private javax.swing.jcombobox statebox; // end of variables declaration//gen-end:variables }
try using removeallitems() method. should call function before start adding items combo box. code should this
/** *do here **/ jcombobox.removeallitems(); /** *do else here **/
Comments
Post a Comment