N
N
nano_e_t_42016-11-08 15:01:34
Java
nano_e_t_4, 2016-11-08 15:01:34

How to change the listener?

Hello everyone
, please tell me how to change the listener of the button?
found this option

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 of them need to change the listener. I don't want to do a loop in a loop

thanks

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
RoverWhite, 2016-11-08
@nano_e_t_4

Well, if you don’t want to go through all ActionListeners on all buttons, you could implement an ActionListener that would be hung on all your buttons, and in turn would call the necessary actions along the chain. Let's say he would have one variable, by changing the value of which, it would be possible to change the final actions called.

D
Dmitry Alexandrov, 2016-11-08
@jamakasi666

but I have 100 buttons and all of them need to change the listener.

Um, maybe the problem lies in the fact that there are so many buttons and it’s worth finding another solution?)
In general, the option will be simpler if you inherit from the button and make it a new method like bu1[i][j].switchActionListeners(ActionListener al).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question