Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adding JRadioButton to RadioButton group

Good day

Would like to know how to add radiobuttons to a ButtonGroup by dragging and dropping components on to the frame.

JRadioButton male = new JRadioButton("Male");
JRadioButton female = new JRadioButton("Female");
ButtonGroup bg = new ButtonGroup();
bg.add(male);
bg.add(female);
pane.add(male);
pane.add(female);

How can I do this by simple dragging and dropping. If I drop the ButtonGroup onto the frame it gets grouped under 'other components' and from there I am not exactly sure how to add radio buttons to the button group.

thanks regards Arian

like image 413
Arianule Avatar asked Jan 09 '12 13:01

Arianule


People also ask

What can you add to a button group?

You'll usually add only radio buttons to a button group, but you can add regular buttons or check boxes, too. Button groups have nothing to do with the visual appearance of the buttons on the screen. Instead, button groups simply provide a logical grouping for the buttons.

How do I add a ButtonGroup to a JPanel?

As ButtonGroup is not a component, you cannot add your ButtonGroup to a JPanel. Instead add your buttons to your JPanel, e.g: JPanel jPanel = new JPanel(); ButtonGroup group = new ButtonGroup(); btn1 = new JRadioButton("btn1 ");btn1. setSelected(true); btn2 = new JRadioButton("btn2 "); group.


1 Answers

you have look at HierarchyListener, on hierarchyChanged event you have add a new JRadioButton to the ButtonGroup, carefully with last know isSelected for concrete JRadioButton

like image 200
mKorbel Avatar answered Sep 23 '22 08:09

mKorbel