Consider the overly simplified example below; some of it written with pseudocode for brevity. When I try running this as is, I get a compiler error stating the actionPerformed is already found in my main method. However, if I rename it, say to actionPerformed2 it’s no longer recognized by ActionListener.
Do I need to combine the listeners for both the foo and foo2 methods into a single ActionListener method?? How do I properly differentiate listeners from each other when using multiple listeners in a single class with multiple button object?
I’m just beginning to play with swing components, so I suspect I may not be asking the right questions…but I can always edit as I go. 🙂
public class foo {
declare button1, button2, button3 and panel1
public foo() {
show panel1 with button1 and button2;
}
public foo2() {
show panel1 with button3;
}
public void actionPerformed(ActionEvent e) {
Object source1 = e.getSource();
do some stuff when button1 is clicked
}
public void actionPerformed(ActionEvent f) {
Object source2 = f.getSource();
do some other stuff when button2 is clicked
}
public static void main(String[] args) {
foo myFoo = new foo();
}
}
Use anonymous inner classes for this.
For example, code that you would never write but that illustrates a point:
Myself, I’m using more AbstractActions and less ActionListeners, and have even started using the Command Design Pattern for this in concert with a PropertyChangeListener.
As an example, my latest GUI’s “view” section looks like this:
And a portion of the “control” section looks like so:
Note that I don’t claim to be an expert at this, but am only trying to manage complexity as best I can. There are probably better ways to skin this cat.