Can addition of an ActionListener be short? Can I add arguments to the actionPerformed?
Posted
by Roman
on Stack Overflow
See other posts from Stack Overflow
or by Roman
Published on 2010-03-25T12:48:39Z
Indexed on
2010/03/25
13:03 UTC
Read the original article
Hit count: 362
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?
© Stack Overflow or respective owner