java - How to get ButtonGroup of JRadioButton -
in project, added itemlistener group of jcheckbox , jradiobuttons.
and want when user click on selected jradiobutton, gets deselected.
for method know getting corresponding buttongroup , calling clearselection() method. in itemstatechanged() method have jtogglebutton option=(jtogglebutton)event.getsource();
so option refers either jradiobutton or jcheckbox. have searched cannot able find method buttongroup jradiobutton.
you can use getsource
, check instanceof
. if jradiobutton
cast jradiobutton
, set selected
false. same thing jcheckbox
if(event.getsource instanceof jradiobutton){ jradiobutton btn=(jradiobutton) event.getsource(); btn.setselected=false; } else if (event.getsource instanceof jcheckbox){ jcheckbox chb= (jcheckbox) event.getsource(); chb.setselected=false; }
if want deselect selected 1 can add condition below
if(event.getsource instanceof jradiobutton){ jradiobutton btn=(jradiobutton) event.getsource(); if(btn.isselected()) btn.setselected=false; } else if (event.getsource instanceof jcheckbox){ jcheckbox chb= (jcheckbox) event.getsource(); if(chb.isselected()) chb.setselected=false; }