java - JComboBox dynamically updating values -


i trying update items of combo box xml nodes, nodes when run loop, somehow not able add them items of combobox

public class xmltoexcelgui extends javax.swing.jframe {      public  java.io.file file;      public xmltoexcelgui() {         initcomponents();     }     public void  setprogressbarvalue(string s){          jprogressbar1.setstring(s);      }      @suppresswarnings("unchecked")      private void initcomponents() {          jlabel1 = new javax.swing.jlabel();         jbutton1 = new javax.swing.jbutton();         jlabel2 = new javax.swing.jlabel();         jprogressbar1 = new javax.swing.jprogressbar();         jbutton3 = new javax.swing.jbutton();         jscrollpane1 = new javax.swing.jscrollpane();         jtextpane1 = new javax.swing.jtextpane();         jcombobox1 = new javax.swing.jcombobox();          setdefaultcloseoperation(javax.swing.windowconstants.exit_on_close);          jlabel1.settext("xmltoexcel");          jbutton1.settext("choose file");         jbutton1.addactionlistener(new java.awt.event.actionlistener() {             public void actionperformed(java.awt.event.actionevent evt) {                 jbutton1actionperformed(evt);             }         });          jlabel2.settext("select measures");          jprogressbar1.setstringpainted(true);          jbutton3.settext("get measure data");          jscrollpane1.setviewportview(jtextpane1);          jcombobox1.setmaximumrowcount(30);         jcombobox1.setmodel(new javax.swing.defaultcomboboxmodel(new string[] {}));          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(137, short.max_value)                 .addgroup(layout.createparallelgroup(javax.swing.grouplayout.alignment.leading)                     .addgroup(javax.swing.grouplayout.alignment.trailing, layout.createsequentialgroup()                         .addcomponent(jlabel1, javax.swing.grouplayout.preferred_size, 97, javax.swing.grouplayout.preferred_size)                         .addgap(127, 127, 127))                     .addgroup(javax.swing.grouplayout.alignment.trailing, layout.createsequentialgroup()                         .addcomponent(jprogressbar1, javax.swing.grouplayout.preferred_size, javax.swing.grouplayout.default_size, javax.swing.grouplayout.preferred_size)                         .addgap(117, 117, 117))))             .addgroup(layout.createsequentialgroup()                 .addgap(18, 18, 18)                 .addgroup(layout.createparallelgroup(javax.swing.grouplayout.alignment.leading)                     .addgroup(layout.createsequentialgroup()                         .addcomponent(jbutton3)                         .addgap(0, 0, short.max_value))                     .addgroup(layout.createsequentialgroup()                         .addgroup(layout.createparallelgroup(javax.swing.grouplayout.alignment.leading)                             .addcomponent(jbutton1, javax.swing.grouplayout.preferred_size, 95, javax.swing.grouplayout.preferred_size)                             .addcomponent(jlabel2, javax.swing.grouplayout.preferred_size, 95, javax.swing.grouplayout.preferred_size))                         .addgap(27, 27, 27)                         .addgroup(layout.createparallelgroup(javax.swing.grouplayout.alignment.leading)                             .addcomponent(jscrollpane1)                             .addgroup(layout.createsequentialgroup()                                 .addcomponent(jcombobox1, javax.swing.grouplayout.preferred_size, javax.swing.grouplayout.default_size, javax.swing.grouplayout.preferred_size)                                 .addgap(0, 0, short.max_value)))))                 .addcontainergap())         );         layout.setverticalgroup(             layout.createparallelgroup(javax.swing.grouplayout.alignment.leading)             .addgroup(layout.createsequentialgroup()                 .addcomponent(jlabel1, javax.swing.grouplayout.preferred_size, 46, javax.swing.grouplayout.preferred_size)                 .addgap(18, 18, 18)                 .addgroup(layout.createparallelgroup(javax.swing.grouplayout.alignment.leading)                     .addcomponent(jbutton1)                     .addcomponent(jscrollpane1, javax.swing.grouplayout.preferred_size, 65, javax.swing.grouplayout.preferred_size))                 .addgap(63, 63, 63)                 .addgroup(layout.createparallelgroup(javax.swing.grouplayout.alignment.baseline)                     .addcomponent(jlabel2, javax.swing.grouplayout.preferred_size, 22, javax.swing.grouplayout.preferred_size)                     .addcomponent(jcombobox1, javax.swing.grouplayout.preferred_size, javax.swing.grouplayout.default_size, javax.swing.grouplayout.preferred_size))                 .addgap(28, 28, 28)                 .addcomponent(jbutton3)                 .addpreferredgap(javax.swing.layoutstyle.componentplacement.related, 26, short.max_value)                 .addcomponent(jprogressbar1, javax.swing.grouplayout.preferred_size, javax.swing.grouplayout.default_size, javax.swing.grouplayout.preferred_size)                 .addcontainergap())         );          pack();     }// </editor-fold>                              private void jbutton1actionperformed(java.awt.event.actionevent evt) {                                                   final jfilechooser  filedialog = new jfilechooser();         int returnval = filedialog.showopendialog(this);             if (returnval == jfilechooser.approve_option) {             file = filedialog.getselectedfile();              testvdt call = new testvdt(file);             jtextpane1.settext(file.tostring());              }                }                                              public static void main(string args[]) {         try {             (javax.swing.uimanager.lookandfeelinfo info : javax.swing.uimanager.getinstalledlookandfeels()) {                 if ("nimbus".equals(info.getname())) {                     javax.swing.uimanager.setlookandfeel(info.getclassname());                     break;                 }             }         } catch (classnotfoundexception | instantiationexception | illegalaccessexception | javax.swing.unsupportedlookandfeelexception ex) {             java.util.logging.logger.getlogger(xmltoexcelgui.class.getname()).log(java.util.logging.level.severe, null, ex);         }          java.awt.eventqueue.invokelater(new runnable() {             public void run() {                 new xmltoexcelgui().setvisible(true);             }         });     }       public javax.swing.jbutton jbutton1;     public javax.swing.jbutton jbutton3;     public javax.swing.jcombobox jcombobox1;     public javax.swing.jlabel jlabel1;     public javax.swing.jlabel jlabel2;     public javax.swing.jprogressbar jprogressbar1;     public javax.swing.jscrollpane jscrollpane1;     public javax.swing.jtextpane jtextpane1;     // end of variables declaration                    } 

public class testvdt {      public int setnumberofmeasures = 0;     public string[]  measures = new string[30];      public void setmeasurevalues(string s[], int length) {         vector comboboxitems = new vector();         (int = 1; < length; i++) {             comboboxitems.add(s[i]);         //    system.out.println(s[i]);         }          xmltoexcelgui call = new xmltoexcelgui();          defaultcomboboxmodel defaultcomboboxmodel = new defaultcomboboxmodel(comboboxitems);          call.jcombobox1.setmodel(defaultcomboboxmodel);        }      public testvdt(java.io.file fname) {          try {             file f = new file(fname.tostring());             fileinputstream fis = new fileinputstream(f);             byte[] ba = new byte[(int) f.length()];             fis.read(ba);             vtdgen vg = new vtdgen();             vg.setdoc(ba);             vg.parse(false);             vtdnav vn = vg.getnav();              fileoutputstream fout = new fileoutputstream("e:\\data.xls");              hssfworkbook workbook = new hssfworkbook();             hssfsheet worksheet = workbook.createsheet("data");              row row1 = worksheet.createrow(0);             row1.createcell(0).setcellvalue("measure_id");             row1.createcell(1).setcellvalue("denominator");             row1.createcell(2).setcellvalue("numerator");             row1.createcell(3).setcellvalue("exclusion");             row1.createcell(4).setcellvalue("performance criteria");              //for(int i=0;i<3;i++)             int rowindex = 1;             int j = 1;             vn.toelement(vtdnav.first_child);             {                  if (vn.matchelement("practice_data")) {                     vn.toelement(vtdnav.first_child);                 }                 if (vn.matchelement("provider_data")) {                     vn.toelement(vtdnav.first_child);                 }                  if (vn.matchelement("provider_identification")) {                     vn.toelement(vtdnav.first_child);                  }                 if (vn.matchelement("measure")) {                     vn.toelement(vtdnav.first_child);                     {                         row row = worksheet.createrow(rowindex++);                         int cellindex = 0;                         string temp = vn.tostring(vn.getattrval("measure_id"));                         system.out.println(temp);                          measures[j] = temp;                         row.createcell(cellindex++).setcellvalue(vn.tostring(vn.getattrval("measure_id")));                         system.out.print(" -- ");                          system.out.print(vn.tostring(vn.getattrval("denominator")));                         row.createcell(cellindex++).setcellvalue(vn.tostring(vn.getattrval("denominator")));                          system.out.print(" -- ");                          system.out.print(vn.tostring(vn.getattrval("numerator")));                         row.createcell(cellindex++).setcellvalue(vn.tostring(vn.getattrval("numerator")));                          system.out.print(" -- ");                          system.out.print(vn.tostring(vn.getattrval("exclusion")));                         row.createcell(cellindex++).setcellvalue(vn.tostring(vn.getattrval("exclusion")));                         system.out.print(" -- ");                          system.out.println(vn.tostring(vn.getattrval("performance_rate")));                         row.createcell(cellindex++).setcellvalue(vn.tostring(vn.getattrval("performance_rate")));                         setnumberofmeasures++;                         j++;                     } while (vn.toelement(vtdnav.next_sibling));                  }              } while (vn.toelement(vtdnav.next_sibling));              system.out.println(setnumberofmeasures);             workbook.write(fout);             fout.flush();              setmeasurevalues(measures, setnumberofmeasures);         } catch (exception e) {             system.out.println("exception occurred ==>" + e);         }     }      @suppresswarnings("empty-statement")     public static void main(string[] args) {      }  } 

by default values of jcombo box blank , when function setmeasurevalues() called should update items of combo box not

your xmltoexcelgui variable, call, local setmeasurevalues(...) method. changes made object refers reflected in local object , not on other object of similar type. guess, perhaps want pass method valid reference displayed xmltoexcelgui object. otherwise if doesn't help, you're going have improve question telling , showing more.


yes, right -- you're setting model of combo box in different non-displayed xmltoexcelgui instance. changing state of 1 instance not , should not have effect on another. solution change state of correct displayed instance.

change this:

private void jbutton1actionperformed(java.awt.event.actionevent evt) {                                               final jfilechooser  filedialog = new jfilechooser();     int returnval = filedialog.showopendialog(this);         if (returnval == jfilechooser.approve_option) {         file = filedialog.getselectedfile();          testvdt call = new testvdt(file);           jtextpane1.settext(file.tostring());          }            } 

to this:

private void jbutton1actionperformed(java.awt.event.actionevent evt) {                                               final jfilechooser  filedialog = new jfilechooser();     int returnval = filedialog.showopendialog(this);         if (returnval == jfilechooser.approve_option) {         file = filedialog.getselectedfile();          testvdt call = new testvdt(this, file);  // ****** changed         jtextpane1.settext(file.tostring());          }           

and this

public class testvdt {      public int setnumberofmeasures = 0;     public string[]  measures = new string[30];      public testvdt(java.io.file fname) { 

to like:

public class testvdt {      public int setnumberofmeasures = 0;     public string[]  measures = new string[30];     private xmltoexcelgui gui;      public testvdt(xmltoexcelgui gui, java.io.file fname) {         this.gui = gui; 

this way method can use reference actual displayed gui:

public void setmeasurevalues(string s[], int length) {     vector comboboxitems = new vector();     (int = 1; < length; i++) {         comboboxitems.add(s[i]);     }      //  xmltoexcelgui call = new xmltoexcelgui(); // **** no!      defaultcomboboxmodel defaultcomboboxmodel = new defaultcomboboxmodel(comboboxitems);      // give xmltoexcelgui public method sets      // own combo box's model     gui.setcombomodel(defaultcomboboxmodel);    } 

Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -