How do I use the Button Group Swing control in Java?
Asked Answered
E

6

14

How do I add radio buttons to a button group using NetBeans?

Once I add them, how do I get selected radio button from the button group?

Expunge answered 20/2, 2010 at 5:5 Comment(0)
D
20

I highly recommend reading this excellent tutorial. Here's an excerpt of code from the article that satisfies your question on how to create and add buttons to a ButtonGroup:

JRadioButton birdButton = new JRadioButton(birdString);
birdButton.setSelected(true);

JRadioButton catButton = new JRadioButton(catString);

   //Group the radio buttons.
ButtonGroup group = new ButtonGroup();
group.add(birdButton);
group.add(catButton);

As far as getting which item is selected, you basically need to iterate through the items in the group calling isSelected.

Deviationism answered 20/2, 2010 at 5:15 Comment(1)
The tutorial shows how it is done in plain Java. While the question is specific about Netbeans, the solution is not. Devon's answer is correct if the GUI builder shall be used.Come
S
32
  1. Drag a ButtonGroup from the palette and drop it on your GUI. It will show up under Other Components in the Inspector panel.
  2. Right-click on it and Change variable name to something meaningful.
  3. Now select a radio button in your GUI.
  4. In the Properties panel look for the buttonGroup property.
  5. Click the combo box next to it and select your button group.
Sandbox answered 23/2, 2010 at 20:21 Comment(2)
James, you might consider accepting this answer. I agree that Devon gave the right NetBeans answer.Rachaba
The other one is very helpful, but this should be the answer because it answers the question.Inflationary
D
20

I highly recommend reading this excellent tutorial. Here's an excerpt of code from the article that satisfies your question on how to create and add buttons to a ButtonGroup:

JRadioButton birdButton = new JRadioButton(birdString);
birdButton.setSelected(true);

JRadioButton catButton = new JRadioButton(catString);

   //Group the radio buttons.
ButtonGroup group = new ButtonGroup();
group.add(birdButton);
group.add(catButton);

As far as getting which item is selected, you basically need to iterate through the items in the group calling isSelected.

Deviationism answered 20/2, 2010 at 5:15 Comment(1)
The tutorial shows how it is done in plain Java. While the question is specific about Netbeans, the solution is not. Devon's answer is correct if the GUI builder shall be used.Come
B
2

To select a radio button programmatically, try these:

private final ButtonGroup buttonGroup = new ButtonGroup();

JRadioButton btn01 = new JRadioButton("btn 1");
buttonGroup.add(btn01);
JRadioButton btn02 = new JRadioButton("btn 2");
buttonGroup.add(btn02);
JRadioButton btn03 = new JRadioButton("btn 3");
buttonGroup.add(btn03);
// gets the selected radio button
if(buttonGroup.getSelection().equals(btn01.getModel())) {
 // code
}

// similarly for the other radio buttons as well.
Berey answered 5/3, 2013 at 17:24 Comment(0)
B
1

How to Use Buttons, Check Boxes, and Radio Buttons

ButtonGroup group = new ButtonGroup();
group.add(new JRadioButton("one"));
group.add(new JRadioButton("two"));
//TO FIND SELECTED
//use a loop on group.getElements();
//and check isSelected() and add them
//to some sort of data structure
Buine answered 20/2, 2010 at 5:16 Comment(0)
D
1
private final ButtonGroup agreeDisagree = new ButtonGroup();

    JToggleButton tglbtnAgree = new JToggleButton("Agree");
    tglbtnAgree.setSelected(true);
    tglbtnAgree.setBounds(227, 127, 75, 23);
    agreeDisagree.add(tglbtnAgree);
    contentPane.add(tglbtnAgree);

    JToggleButton tglbtnDisagree = newJToggleButton("Disagree");
    tglbtnDisagree.setBounds(307, 127, 75, 23);
    agreeDisagree.add(tglbtnDisagree);
    contentPane.add(tglbtnDisagree);
Debauch answered 14/3, 2019 at 18:56 Comment(0)
M
0

In your Navigator Pane, under "Other Components", select your button group. Then select the Code tab in the Properties pane. Select the ellipses (...) to edit the section "After-All-Set Code". Enter your code to add buttons to the button group as previously explained above.

For example:

attemptGroup.add(attemptRadio1); attemptGroup.add(attemptRadio2); attemptGroup.add(attemptRadio3);

Macrophage answered 12/7, 2017 at 19:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.