开发者

How to manage two JRadioButtons in java so that only one of them can be selected at a time

开发者 https://www.devze.com 2022-12-20 04:37 出处:网络
How to manage two JRadio开发者_开发问答Buttons in java so that only one of them can be selected at a time? Is there any method in java to take care of this or you need to build your own logic?You have

How to manage two JRadio开发者_开发问答Buttons in java so that only one of them can be selected at a time? Is there any method in java to take care of this or you need to build your own logic?


You have to add them in a ButtonGroup

ButtonGroup group = new ButtonGroup();
group.add(birdButton);
group.add(catButton);

Ensure you add this code after the buttons are created using the new JRadioButton constructors, as appropriate.


My java is rusty but if i remember correctly you have to use the ButtonGroup class. Add your radio buttons to ButtonGroup object. I think it will look like this.

ButtonGroup buttonGroup = new ButtonGroup();
buttonGroup.add(new JRadioButton('Label', false));
buttonGroup.add(new JRadioButton('Label2', true));

Hope this helps. I have abandoned Java years ago.

0

精彩评论

暂无评论...
验证码 换一张
取 消