I just started to learn how to use action listeners. To my understanding it works in the following way:
There are some classes which contains \"addActionLis
The way I have always found useful (for navigation purposes) is to create an anonymous inner class which then delegates to the outer class:
listenedObject.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
listenedObject_actionPerformed(evt);
}
});
private void listenedObject_actionPerformed(ActionEvent evt) {
//Your handling goes here
}
It is then much easier to get to your handling code in an IDE using a structural lookup (CTRL+F12 in IDEA, CTRL+O in Eclipse).
The problem of using a single class (like a GUI MyCoolPanel) as the common listener to a bunch of its components (buttons etc) is that the actionPerformed method then has a lot of ugly if-else comparisons to figure out which button has actually been pressed - not very OO at all!
You certainly should not get overly worried about the performance aspects of this kind of thing - they are likely to be negligible in the extreme! Premature optimization is famously a bad thing