I have a big table containing a button in each cell. These buttons are very similar and do almost the same. If I add an action listener to every button in this way:
tmp.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent evt) {
proposition = proposition + action;
SwingUtilities.invokeLater(new Runnable() {
public void run() {
updatePropositionPanel();
}
});
}
});
Actually, every action listener differ from all others by the value of the action
. proposition
and updatePropositionPanel
are a field and a method of the class.
First i thought that I can make it shorter if I do not use inner classes. So, I decided to program a new ActionListener class. But than I realized that in this case "proposition" will not be visible to the instances of this class.
Then I decided to add the actionPerformed method to the current class and do that:
addActionListener(this)
. But than I realized that I do not know how give arguments to the actionPerformed method.
So, how does it work. Can I add an action listener in a short and elegent way?
ADDED:
I liked the idea to program an inner class with a constructor which can take some arguments and actioPerformed method, which can use arguments given in the constructor. I started to do so and then realized that it creates a conflicts with other inner anonymous classes (used like in the above given code). So, I think I will create another class (not inner one).