Reputation: 323
I am trying to get the names of all selected JRadioButtons from my graphics interface. As such I create the allFacilities array in which I include all of my JRadioButtons.
The first for loop serves to find the number of selected radio buttons.
The second for loop aspires to get the name of each selected button.
When checking what the .getName() returns:
System.out.println("A##" + button.getName());
, only null is returned for all cases.
Here is my code:
JRadioButton[] allFacilities = {restaurant, laundry, parking};
int selectedFacilitiesCounter = 0;
for(JRadioButton check : allFacilities) {
if(check.isSelected()) {
selectedFacilitiesCounter += 1;
}
}
String[] selectedFacilities = new String[selectedFacilitiesCounter];
int index = 0;
for(JRadioButton button : allFacilities) {
if(button.isSelected()) {
System.out.println("A##" + button.getName());
switch(button.getName()) {
case "restaurant":
selectedFacilities[index] = "restaurant";
break;
case "laundry":
selectedFacilities[index] = "laundry";
break;
case "parking":
selectedFacilities[index] = "parking";
break;
default:
System.out.println("Facility Not Found");
}
index += 1;
}
}
Does anybody have any ideas on how I can solve my problem?
Upvotes: 0
Views: 738
Reputation: 2346
I believe that what you want is this:
JRadioButton button = new JRadioButton("test");
System.out.println(button.getText());
Which will print test
.
The method getName
retrieves the name
of the component, which you should've set with setName
, which I believe you didn't.
Upvotes: 2