Ciara
Ciara

Reputation: 197

How do I set the location of Radio Buttons?

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

Upvotes: 1

Views: 5107

Answers (4)

CrustyCrustecean
CrustyCrustecean

Reputation: 30

I had the same issue. Just do

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

Upvotes: -1

user4955518
user4955518

Reputation: 1

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

Example:

Duck.setBounds(75, 100, 75, MAX_ENTRIES);

Upvotes: -2

nsgulliver
nsgulliver

Reputation: 12671

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);

Upvotes: 2

David Lavender
David Lavender

Reputation: 8331

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.

Upvotes: 0

Related Questions