How to change the listener?

Hello
Plz tell me how to change the listener from buttons?
found here such variant
ActionListener[] listeners = bu1[i][j].getActionListeners();
for (ActionListener l : listeners) {
bu1[i][j].removeActionListener(l);
}
bu1[i][j].addActionListener(new ActionListener() {
...
});

but I have 100 buttons, and all you need to change the listener. I do not want to cycle in a loop

thank you
July 2nd 19 at 18:27
3 answers
July 2nd 19 at 18:29
Solution
Well, if you don't want to sort through all the ActionListeners on buttons, it would be possible to implement an ActionListener which hung all your buttons, and he in turn the chain would cause the desired action. Let's say he would have one variable, changing the value of which could change the outcome of the invoked action.
this is something to think about, thanks - margarette_Wiso commented on July 2nd 19 at 18:32
July 2nd 19 at 18:31
but I have 100 buttons, and all you need to change the listener.

Um, maybe the problem just lies in the fact that so many buttons and should find another solution?)
In General, the simpler version would be if hotnakedbeach of the button and make it a new method of type bu1[i][j].switchActionListeners(ActionListener al).
this is not an option. one hundred buttons should be. very optimally turns out in General - margarette_Wiso commented on July 2nd 19 at 18:34
July 2nd 19 at 18:33
There is no standard solution, you will have to write it yourself.
I came up with this structure:
Code
import java.awt.*;
import java.awt.event.*;
import java.util.*;

class ButtonsGroup {
 private ActionListener sharedListener;
 private final Set<button> buttons;

 public ButtonsGroup(sharedListener ActionListener, Collection</button>

Want to I fix designers, builders and others
well, this is overkill in two cycles - margarette_Wiso commented on July 2nd 19 at 18:36
: how different? You want that action would have happened by themselves? Or whatever platform hid them from you? Somehow from these two cycles does not go away. - margarette_Wiso commented on July 2nd 19 at 18:39

Find more questions by tags SwingJava