Java - AWT/Swing

JRadioButton (...and ButtonGroup)

The example-code produces two radiobuttons which are part of a group:


// produce two RadioButtons:
JRadioButton rb1 = new JRadioButton("Button 1", false);
JRadioButton rb2 = new JRadioButton("Button 2", false);
// assign both RadioButtons to a ButtonGroup, with that only one radiobuttons can be chosen at time:
ButtonGroup bg = new ButtonGroup();
bg.add(rb1);
bg.add(rb2);