java - JCheckBox does'nt trigger isselected -


i have jpanel, contains jpanel , jbuttons. in inner jpanel have jcheckboxes selected. have added actionlistener jbuttons check, if each of jcheckboxes changed unselected action performs. doesn't work. code, problem be?

    public limitpanel(string[] dates, int column) {      gridlayout layout = new gridlayout(1 + dates.length, 0);     setlayout(layout);     setcomponentorientation(componentorientation.right_to_left);     setvisible(true);      this.dates = dates;      checks = new jcheckbox[dates.length][column-1];      (int = 0; < dates.length; i++) {          (int j = 0; j < column - 1; j++) {             checks[i][j] = new jcheckbox();             checks[i][j].setselected(true);             checks[i][j]                     .setcomponentorientation(componentorientation.right_to_left);             add(checks[i][j]);          }     } } public jcheckbox[][] getchecks() {     return checks; } 

in main clas, have method contains:

resultset e = connect.select("invi", "*");         try {             while (e.next()) {                 final int inviid = e.getint("inviid");                     jpanel pn = new jpanel();                 pn.setsize(d.width, d.height);             pn.setlocation(0, 0);                     pn.setlayout(new boxlayout(pn, boxlayout.page_axis));                      lp = new limitpanel(st, 6);                     pn.add(lp);                        jbutton sabt = new jbutton("  ثبت  ");                     sabt.addactionlistener(new actionlistener() {                          @override                         public void actionperformed(actionevent arg0) {                             system.out.println("saaaaaaaaaaaaaaaabt");                             jcheckbox[][] jcb = new jcheckbox[lp.getdates().length][5];                             jcb = lp.getchecks();                             (int = 0; < lp.getdates().length; i++)                                 (int j = 0; j < 5; j++) {                                     if (!jcb[i][j].isselected()) {                                         system.out.println("naaaaaaaaa");                                         connect.insertlimit(inviid, (lp                                                 .getdates())[i], j+"");                                     }                                  }                          }                     });                     pn.add(sabt);                      panels.add(pn);                  }             } catch (sqlexception e1) {                 e1.printstacktrace();             }             setcontentpane(panels.get(p));             revalidate(); 

i edited contain ever necessary, problem system.out.println("saaaaaaaaaaaaaaaabt"); works when press button ever checkboxes system.out.println("naaaaaaaaa"); never works.

your using while (while (e.next()) {) build portions of program. within creating new reference lp , jbutton, sabt, on each iteration.

your actionlistener able reference last instance of lp created. why actionperformed doing think should be...

think this...if do...

lp = new new limitpanel(st, 6); lp = new new limitpanel(st, 6); lp = new new limitpanel(st, 6); lp = new new limitpanel(st, 6);  jcheckbox[] = lp.getchecks(); 

which instance of lp have obtained check boxes from??

updated more details

// create new instance of "limitpanel" lp = new limitpanel(st, 6); // can check using hashcode of object... system.out.println(lp.hashcode()); pn.add(lp);  jbutton sabt = new jbutton("  ثبت  "); sabt.addactionlistener(new actionlistener() {      @override     public void actionperformed(actionevent arg0) {         /*...*/         // use ever last assigned "lp"         jcb = lp.getchecks();         // can check using hashcode of object...         system.out.println(lp.hashcode());         /*...*/     } }); 

if wanted ensure actionlistener using particular instance of limitpanel, should pass reference special instance of actionlistener...

for example...

lp = new limitpanel(st, 6); // can check using hashcode of object... system.out.println(lp.hashcode()); pn.add(lp);  jbutton sabt = new jbutton("  ثبت  "); sabt.addactionlistener(new limitactionhandler(lp)); 

and limitactionhandler...

public class limitactionhandler implements actionlistener {      private limitpanel limitpane;      public limitactionhandler(limitpanel limitpane) {         this.limitpane = limitpane;     }      @override     public void actionperformed(actionevent arg0) {         /*...*/         // use ever last assigned "lp"         jcb = limitpane.getchecks();         // can check using hashcode of object...         system.out.println(limitpane.hashcode());         /*...*/     } } 

as stated in comments, think it's bad idea expose jcheckboxes limitpanel, allows other parts of application unrestricted access objects, don't need there work...

jcheckbox[] jcb = limitpane.getchecks(); (jcheckbox cb : jcb) {     cb.setselected(false); //... } (jcheckbox cb : jcb) {     cb.getparent().remove(cb); //... } 

this dangerous. can argue application won't these things, can't stop happening...


Comments

Popular posts from this blog

java - JavaFX 2 slider labelFormatter not being used -

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -