setSelected a specific jradiobutton in a buttongroup based on action command

Neon Shri picture Neon Shri · Jan 16, 2012 · Viewed 9.9k times · Source

i want to setSelected a speicfic jradiobutton in a buttongroup based on the actionCommand(/name of the specific jradiobutton).

it could be done usind .setSelected(true)

for example,

JRadioButton rabbitButton = new JRadioButton("rabbit");
 rabbitButton .setActionCommand("rabbit");
JRadioButton pigButton = new JRadioButton("pig");
 pigButton .setActionCommand("pig");

ButtonGroup group = new ButtonGroup();

group.add(rabbitButton);
group.add(pigButton);

now.. without ,

{ rabbitButton.setSelected(true);} NOR {group.setSelected(rabbitButton.getModel,true)}

is there a way to setSelected(true) rabbitButton based on the action command()?

Answer

Spycomb picture Spycomb · Jul 13, 2014
yourButtonGroup.clearSelection();
yourRadioButton.setSelected(true);