1

I want to create two radio buttons for the "Gender" label. Here is what I got:

maleRB = new JRadioButton("Male", true);
femaleRB = new JRadioButton("Female", false);

radioGroup = new ButtonGroup();
radioGroup.add(maleRB);
radioGroup.add(femaleRB);

But I have no idea on how to set their locations. Do you set the location the same as how you set the location of JLabel/JTextField, etc...? Here is the photo. I want to place the radio buttons beside the label "Gender".

IMG

Community
  • 1
  • 1
Ciara
  • 197
  • 1
  • 3
  • 11

4 Answers4

2

It generally depends on which layout you are using, but one way to add the radio button group is via JPanel as i mentioned in the comment earlier.

JRadioButton maleRB   = new JRadioButton("Male"  , true);
JRadioButton femaleRB    = new JRadioButton("Female"   , false);

ButtonGroup bgroup = new ButtonGroup();
bgroup.add(maleRB);
bgroup.add(femaleRB);

JPanel radioPanel = new JPanel();
radioPanel.setLayout(new GridLayout(r, c)); // here r could be 1 and c could be 2 
radioPanel.add(maleRB);
radioPanel.add(femaleRB);
nsgulliver
  • 12,655
  • 23
  • 43
  • 64
0

Just add them to a JPanel the same way you would anything else.

ButtonGroup just wires their events together so that only one can be selected at once. It isn't a graphical Swing component itself.

David Lavender
  • 8,021
  • 3
  • 35
  • 55
-1

I had the same issue. Just do

button.setbounds(x,y,z,w); // your coordinates,length and width button.setLayout(null);

-2
YourButton.setBounds(X, Y, Z, MAX_ENTRIES);

Example:

Duck.setBounds(75, 100, 75, MAX_ENTRIES);
Sam
  • 7,252
  • 16
  • 46
  • 65