0

I intend to write a program where I will give the user a choice to choose from a 8*8 matrix. Because my reputation is below 10 I can not include an image but rest assured its just a normal 8*8 matrix. I plan to visualize it in my Java program with 8*8=64 radio buttons. the user can choose only one radio button at a time so it means all 64 buttons will be of the same button group.

Now, how can I manage the action listeners? it is impossible (really tiresome and boring) to set up 64 individual action listener for each of the 64 radio buttons. since all 64 radio buttons are in the same button group, is there any way I can set up only one event listener to check which button is selected?

If any of my given info is unclear then please let me know :)

PS: I am using Netbeans design tools

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
MD Yousuf Azad
  • 13
  • 1
  • 1
  • 5
  • 1
    *"is there any way i can set up only one event listener to check which button is selected?"* - Yes. Without code it's a little difficult to provide a suggestion, but essentially, you would create an instance of your ActionListener and setting to each button as you create them – MadProgrammer Jul 15 '13 at 11:28
  • *"bcz .. pls"* 1) Please use the correct spelling for words like 'you', 'your' & 'please'. This makes it easier for people to understand and help. 2) Please add an upper case letter at the start of sentences. Also use a capital for the word I, and abbreviations and acronyms like JEE or WAR. This makes it easier for people to understand and help. – Andrew Thompson Jul 15 '13 at 12:45
  • @andrewThompson Sorry, I'm new :( – MD Yousuf Azad Jul 15 '13 at 14:29
  • No need for apologies, and a 'will try in future' is usually considered better. :) – Andrew Thompson Jul 15 '13 at 14:40

3 Answers3

1

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());
        }
    };
vels4j
  • 11,208
  • 5
  • 38
  • 63
  • i want to code my program in netbeans design tools(for the convenience of easy desing), how can i integrate this to the generated code on netbeans? – MD Yousuf Azad Jul 15 '13 at 12:27
  • @MDYousufAzad _for the convenience of easy design)_ sounds like the tool poses the problem, not so convient after all except for the simplest possible contexts ;-) – kleopatra Jul 15 '13 at 13:28
0

The action listener gets passed an ActionEvent. You can make one listener, bind it to all the buttons, and check the event source with getSource():

void actionPerformed(ActionEvent e) {
   Object source = e.getSource();
   ...
}
kiheru
  • 6,588
  • 25
  • 31
  • are you talking about this? 'radioButton.addActionListener(ListenerClass);' if it is, then i have to bind every radioButton separately, right? is there no way where i can do this only once and effect all the radioButtons. they are all bound by the buttonGroup, so i guess this should be doable right? – MD Yousuf Azad Jul 15 '13 at 12:28
  • @MDYousufAzad Yes. You can call that for all the buttons in a loop over `buttonGroup.getElements()` (or, if you create your buttons in a loop, then you can do it directly there), but all of them can share the same listener object – kiheru Jul 15 '13 at 12:36
  • buttonGroup.getElements() this method may come useful. tnx – MD Yousuf Azad Jul 15 '13 at 14:27
  • Is actionPerformed() called when a button is deselected? Meaning that selecting a new radio button in the group would fire two events: one for the button just deselected, one for the button just selected. – mins Sep 12 '14 at 16:47
  • @mins No. If you want to listen to that too, you can use an `ItemListener`. – kiheru Sep 12 '14 at 16:57
0

I think you are implementing the radio buttons like this:

JRadioButton radioButton = new JRadioButton("TEST");

If you are doing like this you have to set a ActionListener to each of your buttons (for example initialize and set the ActionListener in a for-loop) with this statement:

radioButton.addActionListener(this) (if you implement ActionListener in the same class)

At last you can go to your actionPerformed(ActionEvent e) method and get the source with e.getSource and then do something like if else to get the right RadioButton:

if(e.getSource == radioButton1)
{
  // Action for RadioButton 1
}
else if(e.getSource == radioButton2)
{
  // Action for RadioButton 2
}
...
marc3l
  • 2,525
  • 7
  • 34
  • 62