Reputation: 31
I have a buttongroup of 5 radio buttons and I need to check if at least one of them is selected or not.
Is this right?
buttonGroup1.isSelected(ButtonModel);
But what is ButtonmModel
here?
Upvotes: 3
Views: 50523
Reputation: 9
<html>
<body>
<h3>Using the <i>checked property</i> of the radio button to check whether a radio button is selected</h3>
<input type = "radio" name = "radio" value = "10" id = "radio1"> 10 </input> <br>
<input type = "radio" name = "radio" value = "20" id = "radio2"> 20 </input> <br>
<input type = "radio" name = "radio" value = "30" id = "radio3" /> 30 </input>
<p id = "output"> </p>
<button onclick = "getSelectedRadio()"> Check radio </button>
<script>
let output = document.getElementById("output");
function getSelectedRadio() {
let radioButtons = document.getElementsByName('radio');
for (let radio of radioButtons) {
if (radio.checked) {
output.innerHTML = "The radio button is selected and it's value is " + radio.value;
}
}
}
</script>
</body>
</html>
Upvotes: 0
Reputation: 21
Additionally, and more to the point of your question, you can determine if there is a selection among the radio buttons of a button group using the getSelected() method of the ButtonGroup class since it returns null if nothing is selected.
private boolean isSelection(ButtonGroup buttonGroup) {
return (buttonGroup.getSelected() != null);
}
I hope this helps someone!!!
Upvotes: 2
Reputation: 11
You can't do much by way of the ButtonModel when it comes to finding a selected radio button. But a ButtonGroup does have the method getElements() that returns an Enumeration of AbstractButtons. These AbstractButtons can be cast to JRadioButtons during iteration through the Enumeration...
The following method will return the selected JRadioButton of the ButtonGroup passed into it, or return null if none is selected...
private JRadioButton getSelectedRadioButton(ButtonGroup buttonGroup) {
Enumeration<AbstractButton> abstractButtons = buttonGroup.getElements();
JRadioButton radioButton = null;
while (abstractButtons.hasMoreElements()) {
radioButton = (JRadioButton) abstractButtons.nextElement();
if (radioButton.isSelected()) {
break;
}
}
return radioButton;
}
Once you have the selected JRadioButton, it's elementary to access its properties. Suppose you want the text property of whatever button is selected...
String selectedRadioButtonText = getSelectedRadioButton(buttonGroup).getText();
I hope this helps someone!!!
Upvotes: 1
Reputation: 329
Since you only have 5 radio buttons you could manually check all of them (assume the radio buttons are named radio1, ... , radio5):
boolean isRadio1, isRadio2, isRadio3, isRadio4, isRadio5 = false;
if (radio1.isSelected() == true) isRadio1 = true;
if (radio2.isSelected() == true) isRadio2 = true;
if (radio3.isSelected() == true) isRadio3 = true;
if (radio4.isSelected() == true) isRadio4 = true;
if (radio5.isSelected() == true) isRadio5 = true;
Not the most elegant solution, but it will do the trick.
Upvotes: 0
Reputation: 3603
package radiobuttongroup;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.swing.ButtonGroup;
import javax.swing.ButtonModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
public class RadioButtonGroup {
public static void main(String[] args) {
new RadioButtonGroup();
}
private RadioButtonGroup() {
Container contentPane = frame.getContentPane();
contentPane.setLayout(new GridLayout(0, 1));
for (int i = 1; i <= 5; i++) {
JRadioButton radio = new JRadioButton(Integer.toString(i));
ButtonModel buttonModel = radio.getModel();
modelToRadioButton.put(buttonModel, radio);
buttonGroup.add(radio);
contentPane.add(radio);
}
JButton buttonTestSelection = new JButton("Selection Test");
JButton buttonClearSelection = new JButton("Clear Selection");
contentPane.add(buttonTestSelection);
contentPane.add(buttonClearSelection);
buttonTestSelection.addActionListener(new ActionListener() {
@Override public void actionPerformed(ActionEvent arg0) {
ButtonModel buttonModel = buttonGroup.getSelection();
if (buttonModel == null) {
System.out.println("No radio button is selected");
}
else {
if (modelToRadioButton.containsKey(buttonModel)) {
JRadioButton b = modelToRadioButton.get(buttonModel);
System.out.println("You selected button " + b.getText());
}
else {
System.err.println("Weird, unrecognised button model!");
}
}
}
});
buttonClearSelection.addActionListener(new ActionListener() {
@Override public void actionPerformed(ActionEvent e) {
buttonGroup.clearSelection();
}
});
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private JFrame frame = new JFrame("RadioButtonGroup");
private Map<ButtonModel, JRadioButton> modelToRadioButton =
new LinkedHashMap<ButtonModel, JRadioButton>();
private ButtonGroup buttonGroup = new ButtonGroup();
}
Upvotes: -1