get the name of the selected radio button from button group - java

I want to return selected radio button name from button group in java :
ButtonGroup bg = new ButtonGroup();
bg.add(radiobutton1);
bg.add(radiobutton2);
bg.add(radiobutton3);

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

Why do you want the button name? It isn't of much use. You can, however, get the all of the button references using bg.getElements(), and you can get the text of each of those references with button.getText(), both of which are of much more use than the name you gave the button. If you REALLY want the name, you can create the buttons and then call radiobutton1.setName("radiobutton1") and then use getName() on the reference.

Related

how to setChecked radio button in programmatically (radio button created in dynamically ) android

I try that
radioButton.setChecked(true);
but its only work 4th radio button. I try to create radio button dynamically. I create radio buttion within for loop, then store the radio button value. Then restore the radio button value (that means, I have 4 options that time I choose 2nd option and saved it then restore it(setChecked 2nd option) ) but its only setChecked 4th option.
Create radioButton.
for (int k = 0; k < choiceElementList.size(); k++) {
if (choiceElementList.get(k).dataFormatId == 1) {
radioButton = new RadioButton(getContext());
radioButton.setText(choiceElementList.get(k).getDataFormatValue());
radioButton.setLayoutParams(params1);
radioButton.setPadding(0, 5, 0, 5);
Log.e("setid", String.valueOf(choiceElementList.get(k).getId())) ;
radioGroup.addView(radioButton);
}
}
Try to restore that
if(choiceElementList.get(k).getId() == Cons.Id){
radioButton.setChecked(true);
}
First set Ids to your RadioButtons
for (int k = 0; k < choiceElementList.size(); k++) {
if (choiceElementList.get(k).dataFormatId == 1) {
RadioButton radioButton = new RadioButton(getContext());
// Set ID to Radio Button
radioButton.setId(k);
radioButton.setText(choiceElementList.get(k).getDataFormatValue());
radioButton.setLayoutParams(params1);
radioButton.setPadding(0, 5, 0, 5);
Log.e("setid", String.valueOf(choiceElementList.get(k).getId())) ;
radioGroup.addView(radioButton);
}
}
now just use your RadioGroup to check desire RadioButton with its ID
if(choiceElementList.get(k).getId() == Cons.Id){
radioGroup.check(k); // K will be your ID Set for your desire RadioButton
}
Happy Coding...
according to this code segments, your radioButton variable only refer last created element (radiobutton). That's why it only marks fourth one. You need to get correct reference for radio button what u want.
This is because you're adding all your RadioButton in a RadioGroup. When a RadioButton is checked in a RadioGroup, the othe RadioButton will be unchecked. You can see the RadioGroup documentation say it clearly:
This class is used to create a multiple-exclusion scope for a set of radio buttons. Checking one radio button that belongs to a radio group unchecks any previously checked radio button within the same group.
Intially, all of the radio buttons are unchecked. While it is not possible to uncheck a particular radio button, the radio group can be cleared to remove the checked state.
The selection is identified by the unique id of the radio button as defined in the XML layout file.

How to check condition that 'only one jRadiobutton should be selected'

I am working on details entry form on NetBeans with Java Swings. There are three certain jRadiobutton, of them only one must be selected.
Any methods that could give such power, please suggest. Or might I need to custom my own method to perform check?
You have to create a object of ButtonGroup and add radio buttons to that object.
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);

how to check radio Button on activity load

I have taken a radio group in my layout and I want to check one of the radio button from that radio group by default when activity is initialised. The re is a condition on which any one of the radio button should be checked by default.the code for that is:
if (preferencedPractices == null) {
allRadioBtn.setSelected(true);
System.out.println("preferences are null");
} else {
prefRadioBtn.setSelected(true);
System.out.println("there are some preferences");
}
Here if preferencedPractices is null, I want allRadioBtn checked by default else i want prefRadioBtn checked bydefault. But due to some reason this is not working. None of the button is checked when activity is started. I have to check any of the button..Please help.
In your xml file you can always have a radio button as checked using:
android:checked="true"
In your java code you can do the same using:
rb1= (RadioButton) findViewById(R.id.rb1);
rb2 = (RadioButton) findViewById(R.id.rb2);
rb1.setChecked(true);
Just keep in mind that this will not work alone in RadioGroup you have to individually initialize the radiobutton which you want to set as checked.

how to select a radiobutton in buttongroup in Java

I created several radio buttons in a button group but I cannot understand how to set necessary radio button in a code. My program reads the personal information in a file and shows it in the form (one person at a time). The personal information includes a marital status apart from name and other data, so I use radio buttons for the marital status.
This is my code (I used class Person with public enum MaritalStatus {SINGLE, MARRIED, WIDOW};):
buttonGroup = new ButtonGroup();
for (Person.MaritalStatus c : Person.MaritalStatus.values()) {
JRadioButton radioButton = new JRadioButton(c.name());
buttonGroup.add(radioButton);
if (c == mStatus) {
radioButton.setSelected(true);
}
radioButtonPanel.add(radioButton);
}
So, I have a group of radio buttons:
SINGLE, MARRIED, WIDOW
If user changes the person, the program must update the info about the marital status of the next person in a file. In other words, I don't know how to choose necessary radio button. The method of button group setSelected(buttonModel, bool) requires the buttonModel/radioButton name, but I don't have it in the code

How to disable a JRadioButton inside a ButtonGroup in Java

i have four JRatioButtons inside a ButtonGroup in Java. The two first are enabled and the other two are disabled. If one specific JRatioButton is selected i need to enable the two disabled JRatioButtons.
Im trying this to find the state of the buttons and enable the disabled ones, apparently i found the ones with the disable state but doesnt change that state.
private void activateButtons() {
Enumeration<AbstractButton> elements = myButtonGroup.getElements();
while (elements.hasMoreElements()) {
AbstractButton button = (AbstractButton)elements.nextElement();
if (button.isEnabled()) {
System.out.println("This button is disabled! The text of the button is: '" + button.getText() + "'");
button.setEnabled(true);
}
}
}
Im getting the text of the disabled buttons, but i cant disable them.
Any help? Thanks!
I don't know if you any problems in finding the reference of the radio buttons in the second group or you just cannot disable the radio buttons.
For the first question, it is simple, you just keep the reference of the radio buttons in the second group.
For the second question, you need to subclass a JRadioButton because I found you can not simply call disable for an object of radio button.
The code sample of the sub class would be like this.
this.editable = editable;
if (editable) {
this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
super.enableEvents(Event.MOUSE_DOWN | Event.MOUSE_UP);
} else {
this.setCursor(CursorFactory.createUnavailableCursor());
super.disableEvents(Event.MOUSE_DOWN | Event.MOUSE_UP);
}
Try this, it works.
AbstractButton button = ...
button.getModel().setEnabled(true/false)
Ya, you setEnabled(true) to an enabled RadioButton.
So here the edited, hope can help someone.
private void activateButtons()
{
Enumeration<AbstractButton> elements = myButtonGroup.getElements();
while (elements.hasMoreElements())
{
AbstractButton button = (AbstractButton)elements.nextElement();
if (button.isEnabled()) // if enabled (true)
{
System.out.println("This button is disabled! The text of the button is: '" + button.getText() + "'");
button.setEnabled(false); // set it disabled (false)
}
}
}
Thanks #Hannibal, your post saved my day.

Categories