I’m new to Swing and need some help with action listeners. I have seen them used like this example:
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Do something
}
});
However I want to do something more like this:
button.addActionListener(myFunc);
public void myFunc(ActionEvent e)
{
// Do something
}
Is this possible?
Two possible approaches here – either you can just call your myFunc method directly from within the first example you give:
…Or you can define an inner class that implements actionlistener and then use that:
On a futuristic note, when Java 8 hits the shelves (2013, so don’t hold your breath) you’ll be able to do this more concisely using closures.