0

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?

Lampros Tzanetos
  • 323
  • 1
  • 2
  • 20
  • By "name" you mean the displayed text? Did you try `getText()`? If you want to work with names you need first to call `setName(string)` and then you can get it later. – user1803551 Dec 05 '17 at 21:27
  • I'll try the setName(). I mean the name of the JRadioButton variable as in the ones displayed when I initialize the array. – Lampros Tzanetos Dec 05 '17 at 21:28
  • Use `button.getText()` instead. – Rafael Guillen Dec 05 '17 at 21:28
  • If you want the name of the variable, take a look at this thread: https://stackoverflow.com/questions/744226/java-reflection-how-to-get-the-name-of-a-variable – wleao Dec 05 '17 at 21:32
  • You shouldn't do it with the name of the variables itself. The variable names are not preserved in compilation and there's no reason to do it anyway. You have to use stored data. – user1803551 Dec 05 '17 at 21:32
  • I agree with @user1803551. Just go with getText and you'll be fine. – wleao Dec 05 '17 at 21:33
  • Thank you for your help! I used getText() and setName() and both worked fine. I think I'll proceed with getText() though. – Lampros Tzanetos Dec 05 '17 at 21:59
  • Possible duplicate of [Getting text value from a jButton](https://stackoverflow.com/questions/20082051/getting-text-value-from-a-jbutton) – user1803551 Dec 05 '17 at 23:05

1 Answers1

2

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.

wleao
  • 2,316
  • 1
  • 18
  • 17