How to create a ButtonGroup

To create a button group is also a simple task. What we have to do is to create a number of buttons and assign the same ButtonGroup name to all these buttons. To demonstrate this, follow these steps:

  • Select the panel1 Tab we created in the GUI builder tree in the previous example.
  • Press the new JRadioButton on the right toolbar at first only once.
  • Set the Group property of this JRadioButton to radioGroup and pess Save.
  • Twice press new JRadioButton now. As you can see the system knows that there is already a Button-Group.
  • Set the Name property of these buttons to radioButton1, radioButton2, radioButton3.
  • Set the Text properties to RadioButton1, RadioButton2, RadioButton3.
  • Set the Group property of all these buttons to radioGroup.
  • Set the Selected property of the first button to true so that this button is the one initially selected.

Save your buttons and check out your frame.

Now let's come to the Size Ratio property in this context. If you want all three buttons to have the same width of one third of the panel they are laid out set their SizeRatio (X-Multiply-Divide-Ratio) property to 3 as seen in this picture.

SizeRatio Editor

Check your frame and see the result. You can also get to this if you change the layout of your panel to a GridLayout with 3 columns. But, for example, if you want to arrange a JLabel to two fifths width and a JTextField nearby to three fifths, this might be an easier way than to work with GridBagLayout.