0

i have a multiple jradiobutton that is inside a for loop and i am trying to put listener on it and this is what i found:

Action listener for multiple radio buttons

Create two dimensional JRadioButton array like

    JRadioButton[][] jRadioButtons = new JRadioButton[8][];
    ButtonGroup bg = new ButtonGroup();
    JPanel panel = new JPanel();
    panel.setLayout(new GridLayout(8, 8));
    for (int i = 0; i < 8; i++) {
        for (int j = 0; j < 8; j++) {
            JRadioButton btn = new JRadioButton();
            btn.addActionListener(listener);
            btn.setName("Btn[" + i + "," + j + "]");
            bg.add(btn);
            panel.add(btn);
            // can be used for other operations
            jRadioButtons[i][j] = btn;
        }
    }

Here is single ActionListener for all JRadioButtons

ActionListener listener = new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        JRadioButton btn = (JRadioButton) e.getSource();
        System.out.println("Selected Button = " + btn.getName());
    }
};

i kinda understand it but i still have few clarifications:

  1. what's the purpose of two dimensional jradiobutton? i mean i kinda see that it is to set a name for the jradiobuttons but as far as my understanding goes, it's only for display. yes to confirm that that is the jradiobutton you've selected but i don't get what's the purpose of it in putting actionlistener
  2. is the two dimensional jradiobutton really that necessary?
  3. can i just use the name of jradiobuttons

to do something like this:

if(NameOfJRadioButton.isSelected())

{

//some procedures

}

^(i can't seem to convert that into code :/)

if so, how can i do it? or do you have any other suggestions on how to put listener for multiple jradiobuttons? thank you for any of your suggestions :)

Community
  • 1
  • 1
bruh
  • 79
  • 3
  • 13
  • `is the two dimensional jradiobutton really that necessary?` - do you ever reference the array anywhere in your code? `i don't get what's the purpose of it in putting actionlistener` - if you don't have a purpose then why write the ActionListener? You only write an ActionListener if you want to do something when the user clicks on the radio button. – camickr Oct 20 '16 at 23:44
  • @camickr oh that is not my code. i just found it in another stackoverflow post. i just have few clarifications of what is really the purpose of it because i don't seem to get why is it there as well as asking if my idea would work and how – bruh Oct 20 '16 at 23:47

1 Answers1

1

On your first and second point, the reason for the two dimensional array is unknown as it is not your code, but is not necessary at all for the use of JRadioButtons. However it is useful to have all your buttons in some type of array, whether it be an arraylist, or a buttonGroup (swing list for buttons) for checking things with the buttons when an action is called. e.g. on your 3rd point, this array list would allow you to iterate through and check which buttons have been selected and act accordingly.

The purpose for the action listener is for executing an action when the user clicks on a button. The most general use for this is making it so the user is only allowed to select a certain amount of JRadioButtons or to disable them once they have been selected. e.g. at a character selection menu.

Kyal Bond
  • 296
  • 1
  • 12