i intend write program give user choice choose 8*8 matrix. because reputation below 10 can not include image rest assured normal 8*8 matrix. plan visualize in java program 8*8=64 radio buttons. user can choose 1 radio button @ time means 64 buttons of same button group.
now, how can manage action listeners? impossible (really tiresome , boring) set 64 individual action listener each of 64 radio buttons. since 64 radio buttons in same button group, there way can set only one event listener check button selected?
if of given info unclear please let me know :)
ps: using netbeans design tools
create 2 dimensional jradiobutton
array
jradiobutton[][] jradiobuttons = new jradiobutton[8][]; buttongroup bg = new buttongroup(); jpanel panel = new jpanel(); panel.setlayout(new gridlayout(8, 8)); (int = 0; < 8; i++) { (int j = 0; j < 8; j++) { jradiobutton btn = new jradiobutton(); btn.addactionlistener(listener); btn.setname("btn[" + + "," + j + "]"); bg.add(btn); panel.add(btn); // can used other operations jradiobuttons[i][j] = btn; } }
here single actionlistener
jradiobuttons
actionlistener listener = new actionlistener() { @override public void actionperformed(actionevent e) { jradiobutton btn = (jradiobutton) e.getsource(); system.out.println("selected button = " + btn.getname()); } };
Comments
Post a Comment