How to Get Which Jradiobutton Is Selected from a Buttongroup

How do I get which JRadioButton is selected from a ButtonGroup

I would just loop through your JRadioButtons and call isSelected(). If you really want to go from the ButtonGroup you can only get to the models. You could match the models to the buttons, but then if you have access to the buttons, why not use them directly?

How to get value of selected radioButton of buttonGroup

I tried using buttonGroup1.getSelection().getActionCommand()

That approach will work, but for some reason it looks like you manually need to set the action command when you create the button. For example:

JRadioButton maleButton = new JRadioButton( "Male" );
maleButton.setActionCommand( maleButton.getText() );

This acutally seems like a bit of a bug to me since usually the action command defaults to the text if the action command is not set.

get the name of the selected radio button from button group

I think you can loop through the radio buttons, for each button if isSelected() is true then call button.getText() to get the name.

for (AbstractButton button : bg.getElements())
if (button.isSelected())
return button.getText();

How to find that item is selected in a button group?

Reference ButtonGroup: getSelection()

The following example shows how to manage the selection of an item in a button group:

import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRadioButton;

public class MainClass {
public static void main(String[] args) {
JRadioButton dem = new JRadioButton("Bill", false);
dem.setActionCommand("Bill");
JRadioButton rep = new JRadioButton("Bob", false);
rep.setActionCommand("Bob");
JRadioButton ind = new JRadioButton("Ross", false);
ind.setActionCommand("Ross");

final ButtonGroup group = new ButtonGroup();
group.add(dem);
group.add(rep);
group.add(ind);

class VoteActionListener implements ActionListener {
public void actionPerformed(ActionEvent ex) {
String choice = group.getSelection().getActionCommand();
System.out.println("ACTION Candidate Selected: " + choice);
}
}

class VoteItemListener implements ItemListener {
public void itemStateChanged(ItemEvent ex) {
String item = ((AbstractButton) ex.getItemSelectable()).getActionCommand();
boolean selected = (ex.getStateChange() == ItemEvent.SELECTED);
System.out.println("ITEM Candidate Selected: " + selected + " Selection: " + item);
}
}

ActionListener al = new VoteActionListener();
dem.addActionListener(al);
rep.addActionListener(al);
ind.addActionListener(al);

ItemListener il = new VoteItemListener();
dem.addItemListener(il);
rep.addItemListener(il);
ind.addItemListener(il);

JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = frame.getContentPane();
c.setLayout(new GridLayout(4, 1));
c.add(new JLabel("Please Cast Your Vote"));
c.add(dem);
c.add(rep);
c.add(ind);
frame.pack();
frame.setVisible(true);
}
}

See the Java Tutorial How to Use the ButtonGroup Component for more information.

Get All JRadioButton from a ButtonGroup

Finally i found the solution, i think there are a way to return Enumeration<AbstractButton>, so use it to return all the JRadioButton of this ButtonGroup

//Convert Enumeration to a List
List<AbstractButton> listRadioButton = Collections.list(group.getElements());

//show the list of JRadioButton
for (AbstractButton button : listRadioButton) {
System.out.println("Next element : " + ((JRadioButton) button).getText());
System.out.println("Is selectd = " + button.isSelected());
}

//Set the first JRadioButton selected
if(listRadioButton.size() > 0){
listRadioButton.get(0).setSelected(true);
}

How to select a JRadioButton from ButtonGroup?

From the docs:

public void setSelected(boolean b)

Sets the state of the button. Note that this method does not trigger
an actionEvent. Call doClick to perform a programatic action change.

As mentioned here use:

radioBtn.doClick();

JradioButton that is in ButtonGroup to setSelected

A ButtonGroup does not let all of the RadioButtons to be unselected at the same time. You can clear the selection manually via the ButtonGroup#clearSelection method.

Clears the selection such that none of the buttons in the ButtonGroup
are selected.

Get the focus in a ButtonGroup of JRadioButtons to go to the currently selected item instead of first

You might be able to use a FocusTraversalPolicy:

buttons.setFocusTraversalPolicyProvider(true);
buttons.setFocusTraversalPolicy(new LayoutFocusTraversalPolicy() {
@Override public Component getDefaultComponent(Container focusCycleRoot) {
ButtonModel selection = bg.getSelection();
for (Component c: focusCycleRoot.getComponents()) {
JRadioButton radioBtn = (JRadioButton) c;
ButtonModel loopModel = radioBtn.getModel();
if (loopModel == selection) {
return radioBtn;
}
}
return super.getDefaultComponent(focusCycleRoot);
}
});
  • Customizing Focus Traversal: How to Use the Focus Subsystem (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features)
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ButtonGroupFocusTraversalTest {
public JComponent makeUI() {
JPanel parent = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.gridx = 1;
gbc.gridy = 1;
parent.add(new JLabel("Start"), gbc);
gbc.gridx++;
parent.add(new JTextField(10), gbc);

gbc.gridx = 1;
gbc.gridy++;
parent.add(new JLabel("End"), gbc);
gbc.gridx++;
JTextField textField = new JTextField(10);
parent.add(textField, gbc);

gbc.gridx = 1;
gbc.gridy++;
parent.add(new JLabel("Colors"), gbc);

gbc.gridx++;
final Box buttons = Box.createVerticalBox();
parent.add(buttons, gbc);
final ButtonGroup bg = new ButtonGroup();
for (String s : "Red,Orange,Yellow,Green,Blue,Indigo,Violet".split(",")) {
JRadioButton radioBtn = new JRadioButton(s);
buttons.add(radioBtn);
bg.add(radioBtn);
}

gbc.gridx = 1;
gbc.gridy += 2;
gbc.gridwidth = 2;
final JLabel currentValue = new JLabel("none");
parent.add(currentValue, gbc);
gbc.gridy--;
JButton btn = new JButton("Show Button Group Value");
parent.add(btn, gbc);

buttons.setFocusTraversalPolicyProvider(true);
buttons.setFocusTraversalPolicy(new LayoutFocusTraversalPolicy() {
@Override public Component getDefaultComponent(Container focusCycleRoot) {
ButtonModel selection = bg.getSelection();
for (Component c: focusCycleRoot.getComponents()) {
JRadioButton radioBtn = (JRadioButton) c;
ButtonModel loopModel = radioBtn.getModel();
if (loopModel == selection) {
return radioBtn;
}
}
return super.getDefaultComponent(focusCycleRoot);
}
});
return parent;
}
public static void main(String... args) {
EventQueue.invokeLater(() -> {
JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(new ButtonGroupFocusTraversalTest().makeUI());
f.setSize(320, 320);
f.setLocationRelativeTo(null);
f.setVisible(true);
});
}
}

How to save the selected JRadioButton from a ButtonGroup?

ButtonGroup tweetCredibility = new ButtonGroup();

You are defining the ButtonGroup as a local variable. That is only the GraphicalInterface() constructor knows about it.

You need to define the button group as an instance variable so it can be used by any method of the class. So define the ButtonGroup where you define the JRadioButtons.

Also, the surveyFrame.setVisibe(true) statement should be invoked as the last statement of the constructor AFTER all components have been added to the frame.



Related Topics



Leave a reply



Submit