java - How to capture the amount of clicks over a group of loop generated JToggleButtons? -
i have 20 loop-generated jtogglebuttons , need count how many of them active.
private void generarbotones(){ jtogglebutton b; this.panelcuerpo.setlayout(new gridlayout(4,5)); for(int = 1; i<=20; i++){ b = new jtogglebutton(); b.settext(string.valueof(i)); this.panelcuerpo.add(b); b.addactionlistener(new actionlistener() { int clicks = 0; @override public void actionperformed(actionevent ae2){ clicks = clicks + 1; system.out.println(clicks); } public void setcantidadboletas(int clicks){ cantidadboletas = clicks; } }); } } the problem here counts how many times each 1 of them clicked instead of count how many of them selected.
ps. tried use (b.isselected()) b needs final access wasn't solution.
if declare jtogglebutton inside loop, can make final:
for (int = 1; i<=20; i++) { jtogglebutton b = new jtogglebutton(); then can use b.isselected:
b.addactionlistener(new actionlistener() { public void actionperformed(actionevent evt) { if (b.isselected()) clicks++; else clicks--; } }); } clicks have class variable.
Comments
Post a Comment