Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

GUI problem

  Asked By: Leon    Date: Jan 13    Category: Java    Views: 702
  

I have three jRadiobutton in my GUI. However, I could be able to select all of
those at the same time!!!! I am wondering if you know how can I change it to the
way that only one option selects at the same time.

Share: 

 

10 Answers Found

 
Answer #1    Answered By: Hu Chalthoum     Answered On: Jan 13

If you mean that you want to just select  one of them,,you should put the same name for all your 3 radio button like this:
<input type="radio" name="test" value="red">
<input type="radio" name="test" value="blue">
<input type="radio" name="test" value="green">

 
Answer #2    Answered By: Danny Perkins     Answered On: Jan 13

You need to create a ButtonGroup object and then add all radio buttons that work
together as one group!

ButtonGroup group = new ButtonGroup();
group.add(radio1);
group.add(radio2);
...

It then works fine!

 
Answer #3    Answered By: Riley-jack Johnson     Answered On: Jan 13

If you want to select  one of your JRadioButton you need toadd them in one group.ButtonGroup class will do it for you. Look at the sample:
public class RadioButtonDemo{
JFrame f = new JFrame();
Container c = f.getContentPane();
c.setLayout();
//Instances RaidioButtons
JRadioButton btn1 = new JRadioButon();
JRadioButton btn2 = new JRadioButon();
JRadioButton btn3 = new JRadioButon();

ButtonGroup group = new ButtonGroup();
group.add(btn1);
group.add(btn2);
group.add(btn3);

c. add(btn1);
c. add(btn2);
c. add(btn3);

f.pack();
f.setVisible(true);


}

 
Answer #4    Answered By: Kawthar Malik     Answered On: Jan 13

You need to use the ButtonGroup class. See here: java.sun.com/.../ButtonGroup.html

 
Answer #5    Answered By: Anthony Smith     Answered On: Jan 13

1. Create an instance of ButtonGroup.
2. Create instances of JRadioButton.
3. Add each JRadioButton instance to the ButtonGroup instance.

// Create the button group.
bg = new ButtonGroup();
// Create the radio buttons. Select the first one.
jrbFrench = new JRadioButton("French", true);
jrbGerman = new JRadioButton("German");
jrbChinese = new JRadioButton("Chinese");
// Add the radio buttons to the group.
bg.add(jrbFrench);
bg.add(jrbGerman);
bg.add(jrbChinese);

 
Answer #6    Answered By: Edna West     Answered On: Jan 13

You should declare a ButtonGroup and then in initialize() method or your JRadioButton decalation method
you should add your JradioButton to the declared ButtonGroup.
So , you will have something like this :


ButtonGroup btngrp = new ButtonGroup();

....

btnRepayment .add(jrb1);

 
Answer #7    Answered By: Guilherme Silva     Answered On: Jan 13

You have to create a "javax.swing.ButtonGroup" and add your radio buttons to it.

Hope it will help you.

 
Answer #8    Answered By: James Evans     Answered On: Jan 13

You should use ButtonGroup to group your radio buttons.
In below example if jrb1 and jrb2 and jrb3 are JRadioButton instances so group make them as a group so you can just select  one of them.

ButtonGroup group = new ButtonGroup();
group.add(jrb1);
group.add(jrb2);
group.add(jrb3);

 
Answer #9    Answered By: Clarence Nichols     Answered On: Jan 13

ButtonGroup Class , like this

JRadioButton birdButton = new JRadioButton("bird");
JRadioButton catButton = new JRadioButton("cat");
ButtonGroup group = new ButtonGroup();
group.add(birdButton);
group.add(catButton);
birdButton.setSelected(true);

at the same time just one of the catButton or birdButton can be selected

 
Answer #10    Answered By: Geena Ma.     Answered On: Jan 13

You can overcome this problem  using the following code.
buttonGroup1 = new javax.swing.ButtonGroup();
jRadioButton1 = new javax.swing.JRadioButton();
jRadioButton2 = new javax.swing.JRadioButton();
jRadioButton3 = new javax.swing.JRadioButton();
buttonGroup1.add(jRadioButton1);
buttonGroup1.add(jRadioButton2);
buttonGroup1.add(jRadioButton3);
this will create a group where you can select  only one of radio button

 
Didn't find what you were looking for? Find more on GUI problem Or get search suggestion and latest updates.




Tagged: