java - how to identify a button in a group of loop generated buttons? -
i have group of loop generated buttons made code
this.panelcuerpo.setlayout(new gridlayout(4,5)); for(int = 1; i<=20; i++){ final jtogglebutton b = new jtogglebutton(new imageicon("/images/available.png")); panelcuerpo.add(b); b.seticon(new javax.swing.imageicon(getclass().getresource("/images/available1.png"))); b.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent evt){ if(b.isselected()){ b.seticon(new javax.swing.imageicon(getclass().getresource("/images/busy1.png"))); cantidadboletas++; }else{ b.seticon(new javax.swing.imageicon(getclass().getresource("/images/available1.png"))); cantidadboletas--; } system.out.println(cantidadboletas); } }); }
the problem here can't use settext() compare later cause there's no property hide text. how can compare later?
ps. each button has consecutive number, it's easy assign number. real problem lies in put it.
you could:
- use
action
api, lets triggerselected
state of associated button. allows de-couple button underlying "action" should take. take @ how use actionshow use actions more details - use
actioncommand
property ofjbutton
. allows have kind of "identifier" associated button independent of text - use array or
list
maintain reference buttons
Comments
Post a Comment